Asp – Client and Server side validation in asp.net MVC application

asp.net-mvcvalidation

We are starting a new ASP.NET 3.5 MVC application. Following are the requirements for validation:

  • Both client and server side validation.
  • Validation rules in one place.
  • Common scenarios like 'Password' & 'Confirm Password' are addressed.

Options:

  • DataAnnotation (ONLY does server side validation)
  • EL 4.1 Validation Application Block (ONLY does server side validation)
  • xVal Framework
  • Validation Library framework
  • Validator Toolkit Framework
  • OTHERS ?

xVal and 'Validation Library' both can use DataAnnotation and jQuery validation plugin.

If a form has a field which is required for 'Create' but not required for 'Update', which of these frameworks can handle this scenario ?

Please advise which will be the best choice for MVC Client & Server validation ?

Thank You.

Best Answer

I can answer the others question :)

FluentValidation looks interesting. They provide a fluent syntax like:

public class CustomerValidator: AbstractValidator<Customer> {
    public CustomerValidator() {
        RuleFor(customer => customer.Surname).NotEmpty();
        RuleFor(customer => customer.Forename).NotEmpty().WithMessage("Missing first name");
    }
}

It also have some small integration with ASP.NET MVC, where you can add the result of the validation to the ModelState, as shown below:

public ActionResult Save(Customer customer) {
    var validator = new CustomerValidator();
    var results = validator.Validate(customer);
    results.AddToModelState(ModelState, "customer");
}