AI OnAI Off
Hi Bilash
I have not tried it but you might be able to use an IValidator<T>implementation on the form element for this: https://world.episerver.com/documentation/developer-guides/CMS/Content/Validation/
David
Hi,
I have tried the IValidate<T>
and it will not work as your requirement for a property validation. What will happen is, publish button will not be visible even you update your property for a valid value. Error will keep showing. I think you should use ValidationAttribute
. I can share you a code example if use need to understand the implementation of ValidationAttribute.
/Praful
Hi,
I have created the [MaxElements] attribute. You can try that.
Property on the block
[Display(
Name = "Cards",
Description = "Cards for block",
GroupName = CmsGroupDefinitions.Content,
Order = 300)]
[Required]
[MaxElements(4)]
public virtual IList<CardItem> Cards { get; set; }
Then create a class
namespace ABC.Application.Foundation.Attributes
{
using System;
using System.ComponentModel.DataAnnotations;
using System.Web.ModelBinding;
using EPiServer.Core;
using EPiServer.SpecializedProperties;
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class MaxElementsAttribute : ValidationAttribute, IMetadataAware
{
public MaxElementsAttribute(int maxElementsInList)
{
this.MaxCount = maxElementsInList;
}
public int MaxCount { get; set; }
public void OnMetadataCreated(ModelMetadata metadata)
{
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (value == null)
{
return null;
}
if (value is LinkItemCollection)
{
if ((value as LinkItemCollection).Count > MaxCount)
{
return new ValidationResult("Too many Link Items in the collection. Maximum is " + MaxCount);
}
}
else if (value is ContentArea)
{
if ((value as ContentArea).Count > MaxCount)
{
return new ValidationResult("Too many content items in content area. Maximum is " + MaxCount);
}
}
return null;
}
}
}
Thanks
Ravindra
Can we limit the number of options user can select in checkboxes created by SelectMany attribute in edit mode? I have created the checkboxes as explained in https://world.episerver.com/documentation/developer-guides/CMS/Content/Properties/built-in-property-types/Single-or-multiple-list-options/.