MVC Validation
MVC has many possibilities for validation. Depending on the use case, the correct validation should be used. Here is a list of MVC validation possibilities:
- Server side Model-level validation
- Server side Property-level validation
- Client side Model-level validation
- Server side Property-level validation
- Server side custom validation
When validating, a number of problems can exist or a number of design decisions need to be taken:
- Should ActionFilter validation be used?
- How are nullable properties in the model validated? (Model.IsValid client and server side)
- How are nested models validated?
- With a server validation error, should the response be the same with validation errors, a refresh, a redirect or an error response?
- Is it required to unit test the server side validation logic?
- Should the model be separated from the validation logic?
What type of validation possibilities are available for MVC applications (NOT A COMPLETE LIST!):
- Fluent Validation with ASP.NET MVC
- Enterprise Library Validation
- MVC Validation Data Annotations
- FoolProof MVC validation
- jQuery Validation
Fluent Validation with ASP.NET MVC
This is a very good validation library which can be easily extended, the validation logic is easy to test and it supports complex validation logic.
Here’s a simple example.
Simple MVC Application using FluentValidation.MVC4
Enterprise Library Validation
The Enterprise Library validation block provides a strong validation library with lots of features. This library provides many features but is more complex to use compared to the others. One major problem with this validation library is that it provides no client side validation in MVC. Enterprise Library Validation Block provides a good integration for WCF services.
Taken from the Enterprise Library documentation:
You can define validation rules and carry out validation in the following ways:
- By using configuration to define rule sets for specific classes. These rules sets are stored in your application configuration file, and can be created using the graphical configuration tools.
- By adding attributes to members of your classes to define individual rules for public, readable parameters and properties that specify rule sets or individual validation rules. These may be attributes defined within the Validation Application Block that directly target the validators provided with the block, or .NET Data Annotation attributes. The Validation Application Block works with both of these types of attributes.
- By adding code to your classes that perform self validation of the object parameters or properties. This is a useful way to implement very complex validation rules that depend on the environment or external factors.
- By using code to create instances of validators and then execute validation on demand.
Here is a simple example of an MVC application using Enterprise Library validation.
Simple MVC Application using Enterprise Library validation
MVC Validation Data Annotations
This is what comes out of the box with the .NET framework, it works very well but requires extra effort if complex validation is required.
The validation framework can be easily extended. The big advantage with this type of validation, is that it is forward compatible with future versions of the .NET framework. If your application will have a lifecycle of many years, this should be considered when choosing a framework.
Here is a simple example of an MVC application using Data Annotations validation.
Simple MVC Application using Data Annotations validation
FoolProof MVC validation
This validation works well but I would not choose it over any of the other validation possibilities.
jQuery Validation
This is the client side validation. Works well, can be easily extended but can easily be avoided or bypassed.
Comparison of Validation libraries for MVC applications
Links Enterprise Library Validation
http://forums.asp.net/t/1635910.aspx/1
http://www.codeproject.com/Articles/256355/Microsoft-Enterprise-Library-5-0-Introduction-to-V
http://msdn.microsoft.com/en-us/library/ff648831.aspx
http://www.aaronstannard.com/post/2010/05/24/Enterprise-Library-Validation-Block-Part-1.aspx
http://www.codeproject.com/Articles/259327/Integrate-Validation-Block-with-WCF
Links Fluent Validation MVC
http://fluentvalidation.codeplex.com/
https://github.com/JeremySkinner/FluentValidation
http://www.beabigrockstar.com/using-fluent-validation-with-asp-net-mvc-part-1-the-basics/
http://www.beabigrockstar.com/using-fluent-validation-with-asp-net-mvc-part-2-unit-testing/
http://www.beabigrockstar.com/using-fluent-validation-with-asp-net-mvc-part-4-database-validation/
http://www.codeproject.com/Articles/326647/FluentValidation-and-Unity
Links
http://www.asp.net/mvc
http://stackoverflow.com/questions/6807472/fluent-validation-vs-data-annotations
http://www.asp.net/mvc/tutorials/hands-on-labs/aspnet-mvc-4-helpers,-forms-and-validation
http://www.codeproject.com/Articles/577937/A-Beginners-Tutorial-on-Validating-Model-Data-and
http://msdn.microsoft.com/en-us/library/dd410405%28v=vs.98%29.aspx
http://foolproof.codeplex.com/
http://stackoverflow.com/questions/16100300/asp-net-mvc-custom-validation-by-data-annonation