Ok so when I want to have a property be validated, I might write something like this:
[Required]
[StringLength(255)]
[DataType(DataType.EmailAddress)
This SO answer givens an answer for MVC2.
And here is a post for MVC3 and up.
Basically you create an Attribute:
public class MyValidationAttribute: ValidationAttribute
{
public MyValidationAttribute()
{ }
protected override ValidationResult IsValid(
object value, ValidationContext validationContext)
{
...
if (somethingWrong)
{
return new ValidationResult(errorMessage);
}
return null; // everything OK
}
}
The documentation for ValidationAttribute shows how to create your custom validation attribute.