I'm working on a ASP.NET MVC4 web application, specifically in a very typical CRUD interface.
I have a model class such as this:
[MetadataType(typeof(USER_Metadata))]
public partial class USER
{
public class USER_Metadata
{
[Required(ErrorMessageResourceName = "MISSING_EMAIL", ErrorMessageResourceType = typeof(Resources.General.Usuario))]
[StringLength(70, MinimumLength = 3,
ErrorMessageResourceName = "EMAIL_LENGTH_ERROR", ErrorMessageResourceType = typeof(Resources.General.Usuario))]
[DataType(DataType.EmailAddress)]
public string EMAIL { get; set; }
/* other, similarly annotated members */
}
public string EMAIL { get; set; }
}
Which I think is a pretty standard definition of a model class to be used in MVC. I'm using that metadata to validate the user input from a ViewPage, and everything has worked fairly easy.
I'm now trying to validate this conditions on the server side (to massively load users from a file), and I have no idea of how to invoke those validations from outside the view or a controller.
I feel that this is something that probably a lot of people must have tried to do; can my googling be so weak? Is it impossible? Do I have no other choice than to do this in the controller code using ModelState?
I tried using Validator, which I thought was the most obvious-looking way:
var validationResults = new List<System.ComponentModel.DataAnnotations.ValidationResult>();
var isValid = Validator.TryValidateObject(
instance: user,
validationContext: new ValidationContext(user, serviceProvider: null, items: null),
validationResults: validationResults,
validateAllProperties: true);
But that always gives me true. I've also tried using reflection, but to no avail (my knowledge is too shallow in that area).