Dario Quintana

at blogging

NHibernate Validator and Asp.Net MVC

 Download the example here.

Asp.Net MVC has a cool way to add validation errors from the model and display them all into the View. It?s actually using the ModelState. As you may know NHibernate Validator (NHV) is a framework to validate entities, so what about if we let the validation to the framework that can manage it? So the integration of NHV to Asp.Net MVC is easy. I created a new Asp.Net MVC project and added some files to my solution to make they look like this:

First I added the libraries needed to NHV (this libraries we need in case to use NHibernate too, otherwise you don?t need Linfu stuff to get proxies working). The libraries are:

Once the libraries are referenced, NHV need to be initialized, actually in this example that initialization will consist in a Validator Engine provider, capable to be accessed from everywhere into our web-application. A good point do this, is in the Global.asax. The next method InitializeValidator it?s called from the Application_Start.

Then I created a new View Manage.aspx, actually the view is a copy, with modifications, of Register.aspx View. Such View should looks like this one. As you may see, seems like there are some messages of validation, means, a validation that didn?t pass.  Now let?s see the code to explore the minimal code to introduce NHibernate Validator as a Framework to Validate our MVC application.

First of all, our entity Customer, which reflex the view with a Name and a Email properties, should looks like this with the NHV attributes. Remember that NHV can be configured using Attributes (default), Xml or Fluent-Interfaces, and accept mix of configurations too.

Second, we need the integration point between NHibernate Validator and Asp.Net MVC, and that point consist just in a little piece of code that make the validation and modifies the current state of the model. We need just a few lines:

The picture shows an extension method which first of all, get a new ValidatorEngine instance, which is a singleton in whole web application. Actually, to use a ValidatorEngine we need just one instance, because NHV make a lot of useful caching and it configure itself in the way we are using it. Then we validate the entity and get all the InvalidValues of the object. If the entity is in invalid state (break one rule defined), NHV we well generate a InvalidValue array with all errors we should show to the user. Iterate through all the items and we add them all to the ModelState. Once we add one model error, the model is no longer valid.

Once we have our extension, let?s use it.

Validate Method is the extension we made before, and that method is going to modify the ModelState if it?s needed.

And that?s all folks, hope this helps.

  • Pingback: NHibernate Validator y Asp.net MVC at Espacio de Dario Quintana

  • Pingback: ASP.NET MVC Archived Blog Posts, Page 1

  • Cherian Thomas

    For someone new to validators, how is this implementation different from xVal?

  • Cherian Thomas
  • http://darioquintana.com.ar/ Dario

    NHV is a Server Side Validator (yet), and it’s built to easy integration with NHibernate, also, you can use it in non-NHibernate application as CastleValidator, Validation App Block, and so on.

  • Cherian Thomas

    but xval is a suite of validators which include nhibernate validators combined with jquery.validate.
    To quote
    “Version 0.8 adds:

    * Support for NHibernate.Validator (in addition to the existing support for System.ComponentModel.DataAnnotations and Castle Validator)”

  • http://darioquintana.com.ar/ Dario

    Wow, didn’t know it, thanks for the advice!

  • http://hackingon.net Liam McLennan

    Thanks for your example, it is very helpful. Can this example be integrated with NH for the purposes of influencing schema generation and pre-save validation? Ayende has a post (http://ayende.com/Blog/archive/2009/05/01/nhibernate-validator.aspx) that does these things by using ValidatorInitializer.Initialize(configuration, engine) which is not mentioned in the NH Validator documentation.

  • http://malamute.dk/ Jørn Schou-Rode

    Rather than defining an extension method on the Controller class, one could create a custom IModelBinder, whitch knows about NHV, and assign this as the default model binder for your application. This way, you wouldn’t need to manually call the Validate() method.

    I have thrown a code sample together on the following URL. Read the four files from the bottom up (I did not expect the system to order my files alphabetically):

    http://gist.github.com/161479

    Personally, I tend to like this approach better. What do you think?

  • t

    does it support client side validation generation with MVC 2

  • Pingback: ASP.NET MVC Archived Buzz, Page 1