ASP.NET MVC: Adding client-side validation to ValidatePasswordLengthAttribute in ASP.NET MVC 3 Preview 1

This is the second post in what has become a mini-series:

In the previous post we saw how we can add client-side validation to the ValidatePasswordLengthAttribute that is supplied in the default project template.

To recap, the required steps were:

  1. The client-side validation code
  2. ModelClientValidationRules that specify how to hook up to the client-side validation
  3. An adapter class that ASP.NET MVC uses to translate our validation attribute into ModelClientValidationRules and register this with the DataAnnotationsModelValidationProvider

To me, the first couple of steps seemed fine, but the final step seemed to be a slight over-complication. The need to register the adapter also hindered reuse of validation across projects (nothing major, but a mild annoyance). Fortunately this area has received some attention in Preview 1 of ASP.NET MVC 3.

There is now an IClientValidatable interface that the attribute itself can implement. The DataAnnotationsModelValidationProvider checks for this and if present uses it to hook up the client validation. This means that our new steps are:

  1. The client-side validation code!
  2. ModelClientValidationRules that specify how to hook up to the client-side validation
  3. Implement IClientValidatable on our attribute

I’ll skip the first two steps as they’re the same as in the previous post. The final step requires us to add the IClientValidatable implementation to ValidatePasswordLengthAttribute. To do this we just need to add a GetClientValidationRules method that is pretty much the same as the implementation in our adapter class from the previous post:

         public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            yield return new ModelClientPasswordLengthValidationRule(FormatErrorMessage(metadata.DisplayName), _minCharacters);
        }

With this in place we don’t need the adapter class (which simplifies the code) and we don’t need to register anything with the DataAnnotationModelValidatorProvider as it automatically checks whether the attribute implements IClientValidatable (which improves reuse).

All in all I think that this is a rather nice simplification of the validation system for ASP.NET MVC 3!

More on MVC 3 Preview 1: