简体   繁体   中英

Choose the best Client/Server Validation Framework for MVC

Im trying to choose the best server/client validation framework to use with my MVC application. I would like to know what is the best validation framework to use with MVC to do server/client validation.

My options are:

  • Castle validator
  • Microsoft Data Annotations
  • nHibernate Validator

With the upcoming MVC 2, it looks like MS is leaning towards the System.ComponentModel.DataAnnotations library. It's pretty nice - does a lot of code generation for you.

I've been using xVal with great success.

The best thing is you can fully automate validation:

  • put DataAnnotations (or special rules) to your business layer POCO classes (if you have them) or entity classes' metadata buddy classes if you use entities up to controller layer
  • write an ActionFilter that will automatically validate parameters to your controller actions - it's best if all your POCOs (or entities) implement a certain interface that defines Validate() method, filter calls this method and fills ModelState.Errors when validation fails.
  • Add if (ModelState.IsValid) { ... } in your controller action that needs to work differently when model isn't valid
  • Put <%= Html.ValidationMessage(...) %> in your views that will display validation errors
  • Add xVal's client validation to your views if desired

This way you've set up automatic object validation. All your controller actions will do is check Model validity.

Asp.net MVC 2 will have something very similar to xVal already built in the framework so it's up to you which version are you using.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM