views:

1203

answers:

2

Hello,

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.

+2  A: 

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");
}
BengtBe
+1  A: 

I found that xVAL / jquery.validate is a very good combination.

I've written a blog article and a small demo project on how to set everything up, so that

  • Validation rules remain solely in your ASP.NET MVC model
  • You write each validation rule just once, and only in easily testable C# code. There is no JavaScript or other client-side counterpart .
  • All you have to do for each new remote form validation rule is to derive from the base class shown in this article.

    This approach even covers remote validation rules (client-side validation which has to trigger a server-side resource to validate).

Adrian Grigore