Localized property name in validation

Member since: 2011

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
  • 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
  • Member since: 2011

    Perfect mate .. like always supporting..

    Thanks

    #133166 Aug 27, 2015 0:39