AI OnAI Off
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
class ContentItemCountValidationAttribute : ValidationAttribute
{
public ContentItemCountValidationAttribute(int numberAllowed)
{
this.numberAllowed = numberAllowed;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
//CODE FOR RESOLVING DISPLAY NAME
// ------------------------------------
var propertyDefinitionRepository = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IPropertyDefinitionRepository>();
var propertyName = validationContext.MemberName;
var propertyDefinitionId = ((ContentData)validationContext.ObjectInstance).Property[propertyName].PropertyDefinitionID;
var propertyDefinition = propertyDefinitionRepository.Load(propertyDefinitionId);
propertyDefinition.LocalizationService = LocalizationService.Current;
var displayName = propertyDefinition.TranslateDisplayName();
// ------------------------------------
var contentArea = (ContentArea)value;
if (contentArea == null || contentArea.Count < numberAllowed)
{
return new ValidationResult(
string.Format("{0} should have at least {1} content(s), please provide content(s).", validationContext.DisplayName, numberAllowed),
new[] { validationContext.MemberName });
}
return ValidationResult.Success;
}
readonly int numberAllowed;
}
You should be able to cast it to ContentData to get validation of number of content area items to work on properties for blocks as well.
As per this thread https://world.episerver.com/forum/developer-forum/Developer-to-developer/Thread-Container/2015/8/localized-property-name-in-validation/ we can fetch localized validation property display name by casting it to PageData type. But we have this limitation set to BlockData, ContentData Block types. How can we update this line of code for specific block type within content area?