EmailAddressAtribute ignored
c#, data-annotations, validation
Solution
After playing with the overloads available for each method, I found the following overload which includes a parameter called `validateAllProeprties`.
When this is set to `true` the object is property validated.
Validator.TryValidateObject(this, new ValidationContext(this), results, true);
I'm not sure why you wouldn't want to validate all properties, but having this set to `false` or not set (defaults to `false`) will only validate required attributes.
This MSDN article explains.
Problem
I have a class which defines the property `EmailAddress` with the attribute `EmailAddressAttribute` from `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 of `EmailAddress` to be an invalid email (e.g. 'test123'), the `Validate()` method tells me the entity is valid. The `RequiredAttribute` validation is working (e.g. setting `Name` to `null` shows me a validation error). How do I get `EmailAddressAttribute` working in my validator?