I have a class that defines a property EmailAddresswith an attribute EmailAddressAttributefrom System.ComponentModel.DataAnnotations:
public class User : Entity
{
[EmailAddress]
public string EmailAddress { get; set; }
[Required]
public string Name { get; set; }
}
public class Entity
{
public ICollection<ValidationResult> Validate()
{
ICollection<ValidationResult> results = new List<ValidationResult>();
Validator.TryValidateObject(this, new ValidationContext(this), results);
return results;
}
}
When I set the value EmailAddressas an invalid email (for example, "test123"), the method Validate()tells me that the object is valid.
Validation works RequiredAttribute(for example, setting Nameto nullshows me a validation error).
How do I get EmailAddressAttributein my validator?
source
share