Consider I have a these two properties:
public class Test
{
[Required(ErrorMessage = "Please Enetr Age")]
public System.Int32 Age { get; set; }
[Required(ErrorMessage = "Choose an option")]
public System.Boolean IsOld { get; set; }
}
When the user enters for example 15 for Age and choose “Yes” for IsOld, I return an exception that correct Age or IsOld. I’ve used CustomValidation for it, but because my my validation must be static I can’t access to other properties. How can I do this by a DataAnnotation?
You can add data annotations (Custom Validator) to the class itself. In the isValid method of your validation attribute you should then be able to cast the object and test the values that need to be fulfilled.
Example: