Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Localization for Validation property

Vote:
 

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?

var propertyDefinitionId = ((PageData) validationContext.ObjectInstance).Property[propertyName].PropertyDefinitionID;
#204969
Jun 24, 2019 14:00
Vote:
 
    [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. 

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

Thank you for quick help on this.

#204985
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.