Localized property name in validation

Vote:
 

Hi guys,

I built a validation based on "ValidationAttribute" as below code:

	[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
	class ContentItemCountValidationAttribute : ValidationAttribute
	{
		public ContentItemCountValidationAttribute(int numberAllowed)
		{
			this.numberAllowed = numberAllowed;
		}

		protected override ValidationResult IsValid(object value, ValidationContext validationContext)
		{
			var contentArea = (ContentArea)value;
			if (contentArea == null || contentArea.Count < numberAllowed)
			{
				return new ValidationResult(
					string.Format("{0} should have at lesat {1} content(s), please provide content(s).", validationContext.DisplayName, numberAllowed),
					new[] { validationContext.MemberName });
			}
			return ValidationResult.Success;
		}

		readonly int numberAllowed;
	}

The problem is "DisplayName" is not localized property name. Is there anyway to get localized version of property name?

Thanks

#133015
Aug 25, 2015 8:13
Vote:
 

Hi,

I prepared a code snippet:

[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 = ((PageData) 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 lesat {1} content(s), please provide content(s).", validationContext.DisplayName, numberAllowed),
                    new[] { validationContext.MemberName });
            }
            return ValidationResult.Success;
        }

        readonly int numberAllowed;
    }
#133019
Aug 25, 2015 10:40
Vote:
 

Perfect mate .. like always supporting..

Thanks

#133166
Aug 27, 2015 0:39
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.