Try our conversational search powered by Generative AI!

Localization for Validation property


As per this thread 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?

var propertyDefinitionId = ((PageData) validationContext.ObjectInstance).Property[propertyName].PropertyDefinitionID;
Jun 24, 2019 14:00
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
    class ContentItemCountValidationAttribute : ValidationAttribute
        public ContentItemCountValidationAttribute(int numberAllowed)
            this.numberAllowed = numberAllowed;

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
            // ------------------------------------
            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. 

Jun 24, 2019 14:24
Fredrik Gjestvang - May 05, 2020 15:22
Here is a one-liner: page.Property[nameof(page.MyProperty)].TranslateDisplayName();

Thank you for quick help on this.

Jun 25, 2019 6:34
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.