Every once in a while early adoption has a way of making a simple process painful. What makes matters even worse is the lack of documentation. Several times during the spiking process I found myself second guessing whether I was at fault or I found a bug.
First here's an awesome walk through of how to use MVC client side validation. That's right, it's just a blank page at the moment.
Validating on the Server Side
ASP.MVC 2 Preview 1 introduced DataAnnotations to validate a presentation model on the server side. After decorating your model with DataAnnotations validating the presentation model is taken care of by the DefaultModelBinder.
For example, I created a car presentation model class that ensures the make and model are both required and the string length can be no more than 100 characters. I also added a validation to ensure that the year is between 1900 and 2011.
public class Car
{ [DisplayName("Make")] [Required]
[StringLength(100)]
public string Make { get; set; }
[DisplayName("Model")] [Required]
[StringLength(100)]
public string Model { get; set; }
public int Year { get; set; } }
The create action can now ask the ModelState if it is valid. If the ModelState is not valid you can redisplay the view with errors.
public ViewResult Create(Car car)
{ if (ModelState.IsNotValid())
return View(car);
CreateCarCommand.Execute(car);
return View("Success"); }
public static class ModelStateDictionaryExtensions
{ public static bool IsNotValid(this ModelStateDictionary subject)
{ return !subject.IsValid;
}
}
On the view you can display errors in a validation summary or place the errors next to the offending html control.
Validating On The Client Side (MVC 2 Preview 2)
As great as server side validation is, client side validation is the icing on the cake. In the spirit of re-usability DataAnnotations can be reused on the client side. Here is an snippit from ASP.MVC 2 preview 2 release notes:
"The model validator provider class exposes validation metadata to the browser in the form of JSON-serialized metadata that can be consumed by a client-side validation library. The provider class allows you to use other client validation frameworks by writing an adapter that processes the JSON metadata and calls into the alternate client validation library.
ASP.NET MVC Preview 2 includes the jQuery validation library and a client-side validation adapter for that library. The adapter supports the following DataAnnotations namespace validation attributes:
The only kicker is that this is a preview and far from perfect.
To enable client side validation you only need a few things:
- A call to Html.EnableClientSideValidation();
- Three javascript files
- jquery core
- jquery validate
- and MicrosoftMvcJqueryValidation.JS
- A lucky rabbit's foot...
Actually it really wasn't that hard to get going and I imagine it will be allot easier in future releases. Heres a list of a few bugs and annoyances.
- Html.BeginForm and Html.EndForm do not work as pointed out on Hadi Hariri's blog. Since it does not implement IDisposable.
- Html.BeginForm does not seem to generate any JSON or JS needed to validate on the client. I have no Idea why this will not work since it does implement IDisposable..
- So far only Html.BeginForm with out generics works but you can still do some cleaver things to keep things strongly typed.
- Once I had my application up and running I noticed the javascript validations were not firing when I was tabbing over the input boxes. They only started to work after clicking submit.
- If you plan on doing server side validations there seems to be an odd side effect. At least I think it's an odd side effect. I have a view that requires one of the inputs to be validated on the server side. I achieved this by creating a custom validation attribute. Upon returning to the view you are presented with a input box that is colored red and has text explaining the error. Once entering text into the input the textbox returns to normal and the red text disappears. Since the error resides from the server side I would expect the error message to not disappear. I'm still deciding if this is odd behavior or not.
Overall I'm happy with the first release of client side validation for Asp.net MVC. Now that I've had a taste of client side validation in ASP.Net MVC I'm ready to try other validation frameworks.