Friday, September 5, 2008

ASP.NET MVC Preview 5, jQuery, and Validation

I've been given an interesting task - figure out how to provide both client-side and server-side validation of forms, without duplicating the validation logic.

The ASP.NET MVC team just released Preview 5, which, among other bits of goodness, includes the ability to perform server-side validation automagically, and have the results of this validation sent to the view. It's pluggable, much like the rest of the framework.

Meanwhile I went in search of a validation framework (admittedly so I wouldn't have to write my own) and found ValidationFramework - which upon inspection appears to be exactly what I need for server-side validation. It's a well-put-together framework, and has an extensibility model that includes outputing client-side validation for ASP.NET using server controls.

Of course, ASP.NET server controls aren't the best solution when working with the MVC framework, so I considered writing my own adapter layer to generate some Javascript validation from the existing validation rules. I remembered the jQuery Validation plugin and, while I haven't explored it fully, considered that a good starting point.

Well, it seems I wasn't the only one to think so, as this discussion thread shows. I've been in contact with the original poster - Dane O'Connor - and have been reviewing the latest drops of a set of helpers and extensions on both ASP.NET MVC P5 and ValidationFramework that achieves exactly what I initially set out to do on my own!

Now I just have to hope that I'll be allowed by my employer to contribute some code to this project, as we'll be using the system in a production environment. Any improvements we provide would benefit others as well, and since the code is free, I'd like to give something back.

That's it for now.

4 comments:

TheDeeno said...

Just to add. With MVC we don't use the user controls method. We generate javascript based on rules defined in your model. :)

Erik said...

Right =) Definitely the way to go.

Glenn Block said...

Erik

You might want to also check out the Validation Application Block that ships with Enterprise Library.

Erik said...

Thanks Glenn - I have seen the VAB - but I'm looking specifically for something which enables client-side validation without relying on the ASP.NET validation server controls. I could probably write my own translation for the VAB validators to JavaScript but since there are projects doing this already with another validation framework, I don't want to duplicate their efforts.

In other words, I am lazy. =) Lol