I’ve read Phil Haack’s post on custom client-side validation in ASP.NET MVC 2. I want to do the same thing but with the jQuery adapter and using ASP.NET MVC 2 RC (as opposed to MVC 2 Beta that the post uses). Has anyone been able to figure how to do this?
I specially want to implement the password matching validation (i.e. password & confirm password must match). The ASP.NET MVC 2 RC VS.NET project template does show how to implement that on the server-side (using the PropertiesMustMatchAttribute) but not on the client-side.
I assume you already followed Phil Haack’s instructions here http://haacked.com/archive/2009/11/19/aspnetmvc2-custom-validation.aspx) on how to get custom validation working with MS AJAX client validation. To get it to work with jQuery, you’ll need to modify the MicrosoftMvcJQueryValidation.js file:
In the __MVC_CreateRulesForField(validationField) function, you’ll need to add a case statement. Continuing Phil’s example, you’ll need to add:
case “price”:
__MVC_ApplyValidator_Price(rulesObj, thisRule.ValidationParameters[“min”]);
break;
You’ll then need to create the __MVC_ApplyValidator_Price function:
function __MVC_ApplyValidator_Price(object, value) {
}
That should be enough to get Phil’s example working.
Now, regarding your PropertiesMustMatchAttribute validation, it doesn’t look like MVC generates the client-side json validation definition for attributes that decorate classes. Since PropertiesMustMatchAttribute must be used on the model (and not the property), I can’t figure out how to make it trigger client-side validation. Instead, I took a different approach. I created a dummy validation attribute who’s IsValid() overload always returns true, and used this attribute on a property. This is just a dummy attribute that will delegate the validation logic to jQuery validator’s equalTo function. Here’s the dummy attribute:
Here is the custom validator:
the above custom validator needs to be registered in Application_Start() per Phil’s blog:
DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(PropertiesMustMatchClientTriggerAttribute), typeof(PropertiesMustMatchClientTriggerValidator));
Finally, you need to modify the MicrosoftMvcJQueryValidation.js file:
case “equalTo”:
__MVC_ApplyValidator_EqualTo(rulesObj, thisRule.ValidationParameters[“matchField”]);
break;
function __MVC_ApplyValidator_EqualTo(object, elemId) {
}
Now you need to attach the dummy validation attribute to a property:
That should do it.
Creating this dummy attribute is a bit ugly, so I hope someone can come up with a more elegant solution.