I'm writing Validation attribute that sits on the class but inspects the properties of the class. I want it to set a validation message on each of the properties it finds to be invalid. How do I do this?
This is what I have 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
}
}