Introduction to xVal

May 31, 2010 Krzysztof Staszyszyn

At the beginning quick explanation for users who are not familiar with ASP MVC framework yet. ASP MVC is a web applications development framework for .NET which implements Model Viewer Controller design pattern. Development of web application in MVC is a little bit different than in web forms. Instead of thinking in terms of pages and their code behind, you are thinking about views and controllers. Instead of controls placed on pages, you use HTML helpers which generates for you some parts of HTML like input box, dropdown lists, etc. And in short words it is all about MVC framework.

While developing ASP MVC application usually you have well defined model for all business entities in the web application. Having this model, it is only one step to implement server side validation basing on validation attributes attached to this model. Of course, using it is not possible to validate every aspect of data correctness, but you can check in easy way such errors like required values, length of string, range of integers and so on. It is very useful, because after you define set of validation attributes for model class, you don’t have to do once again for each form which uses this model. Then you can focus only on unique validation rules related to given business logic.

If you are working on a Ajax application, which should be as light and as fast as possible, you probably will be disappointed with this kind of validation. Despite most of validation work can be done client side in this case before fields are checked, the form is sent to server and results are available after post back.

You can ask yourself: if we have automatic server side validation, why it is not possible to generate JavaScript code and do the validation on the client too? The answer is that you are wrong, because it is possible to do like this. There is a library, which is available for free and which implements exact the same way of thinking.

xVal library can be attached to each ASP MVC web application. It provides HTML helpers which can be used to generate JavaScript code which will do validation on the client side. JavaScript code is generated completely automatically, and you don’t need to write any JavaScript code line. You can be not familiar with JavaScript at all and you can still use xVal library for client side validation.

xVal has a also a lot of different features and works with different validation methods. One of its advantages is customization. In more advanced scenarios, you can customize xVal library and for example use your custom validation attributes or localize messages which are displayed in Multilanguage applications.

Looking for more details you can visit xVal project homepage which is available on http://xval.codeplex.com. You can also wait for my next posts in this blog, where I will provide quick tutorial how to use xVal and also how to customize it for your needs.

Latest posts