Validation in MVC, Enterprise Library, Fluent Validation, Data Annotations, FoolProof

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!):

  1. Fluent Validation with ASP.NET MVC
  2. Enterprise Library Validation
  3. MVC Validation Data Annotations
  4. FoolProof MVC validation
  5. 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

Links Fluent Validation MVC


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: