In my ViewModels, I use several DataAnnotations to validate form data, usually 2-3 annotations per field.
For example, a field for an email address might look like this:
[Required(ErrorMessage = "Please enter an email address.")]
[Email(ErrorMessage = "That is not a valid email address.")]
public string Email { get; set; }
Now, if someone had to submit the form, both errors will appear in the validation summary. Is there any easy way to specify an order to trigger validation annotations so that if the mandatory validation fails, the email validation fails?
If this is not possible, how is it usually handled? Should I create custom validators for any field containing more than one annotation? Would this be the right way to use annotations where one handles several types of validation?
(I also know that maybe I could combine the required annotation with a custom email, but this is just an example).
source
share