How to dynamically set validation messages on properties with class validation attribute

I am writing a Validation attribute that is on the class but validates the properties of the class. I want him to set an acknowledgment for each of the properties he considers invalid. How to do it?

This is what I got so far:

[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
    public class LinkedFieldValidationAttribute : ValidationAttribute
    {
        private readonly string[] _properiesToValidate;

        public LinkedFieldValidationAttribute(params string[] properiesToValidate)
        {
            _properiesToValidate = properiesToValidate;
        }

        public override bool IsValid(object value)
        {
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);

            foreach (var propertyName in _properiesToValidate)
            {
                var propertyValue = properties.Find(propertyName, false).GetValue(value);
                //if value is invalid add message from base
            }

            //return validity
        }
    }

      

+2


a source to share


1 answer


Use another overloadIsValid

that can return ValidationResult

instead bool

.



0


a source







All Articles