MVC 2 Generic validation. Validate all properties of the model or properties by type
Is there any way to pass to custom valuator the complete model to validate all properties or properties of particular type?
In that case I can see only one solution: to validate model on post action. But may be some one have better more generic idea?
Is it achievable by attributes validation or not?
If it is not achievable using validation attributes in MVC 2.0 is it achievable in 3.0 ?
For example I want to check in any model all string properties if there is first symbol blank.开发者_如何学运维
I would consider using Fluent Validation, not only because it addresses your use case (validation outside of a controller context), but also because it's awesome in many other ways.
From their home page, you could do something like this anywhere in your app:
Customer customer = new Customer();
CustomerValidator validator = new CustomerValidator();
ValidationResult results = validator.Validate(customer);
bool validationSucceeded = results.IsValid;
IList<ValidationFailure> failures = results.Errors;
精彩评论