I’ve come across a couple ways to do validation in MVC. My favorite is the IValidatableObject method. It’s fairly simple to setup.

public class User : IValidatableObject {
    public string Name { get; set; }

    public string Email { get; set; }
    public string Password { get; set; }
    public string VerifyPassword { get; set; }

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) {
        if ((!string.IsNullOrWhiteSpace(Password) || !string.IsNullOrWhiteSpace(VerifyPassword))
        && Password != VerifyPassword)
            yield return new ValidationResult("Passwords must match", new string[] { "Password" });
    }
}

This particular method is only called after all other validations have passed. If you have any DataAnnotations attributes such as [Required] or [StringLength] on your properties that fail then your Validate implementation will not be called.

The order of operations for the IValidatableObject to get called:

  1. Property attributes
  2. Class attributes
  3. Validate interface

If any of the steps fail it will return immediately and not continue processing.

One of the things I don’t like with this particular method is that it puts validation in the model. I currently use the MetadataType attribute on all my models and put all DataAnnotations attributes in there. You can’t implement IValidatableObject on your MetadataType, which is a shame. (Though I could be wrong but testing shows otherwise)

- Ben

Kick It on DotNetKicks.com
Shout it
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Bookmark this on Delicious
Bookmark this on Technorati
Post on Twitter

  9 Responses to “MVC 3′s IValidatableObject”

  1. Nice summary.

    You could put your validation rules in a separate partial class to keep them separate.

    • Agreed. I just liked the idea of having the MetadataType attribute control the validation since that’s pretty much what I’m using it for. :) That way if i apply that same metadatatype to another model the validation goes along with it.

  2. MVC 3′s IValidatableObject…

    Thank you for submitting this cool story – Trackback from DotNetShoutout…

  3. I prefer Steven Sanderson’s approach in Pro ASP.NET MVC 2. From a design patterns/semantic point of view it works.

    Imagine you wanted to use this model in a mobile app. Would those data annotations make any sense?

    Obviously though every project is different. If this is a small-scale app you need out the door asap then this is probably the most economical and “common sense” solution.

    Castle Active Record does already have this facility too.

  4. [...] MVC 3’s IValidatableObject – Ben takes a look at the ASP.NET MVC 3 validation mechanism provided by IValidatableObject implementations, illustrating with a simple example. [...]

  5. My question is how easy is the ValidationContext to use outside of a Web environement. Could you set this up on a Model and use it in a WPF project as well?

    • The ValidationContext relies only on the interface IServiceProvider and is part of the DataAnnotations assembly. Neither of which relies upon MVC itself. You’ll probably have to wire up some of the stuff yourself and I’d have to really look into to give you a good answer.

  6. The IValidatableObject interface is appealing.

    However if I require different validation logic for inserts, updates, and deletes, is there a way of determining what operation is currently in progress?

    Many thanks.

    • Good question. You could probably write your own ModelValidatorProvider to return different IValidatableObjects based on different post data. Checking the “action” route data, for instance, and looking for a different implementation. I’ll see if I can write a post about this soon.

Sorry, the comment form is closed at this time.

   
© 2012 BuildStarted.com Suffusion theme by Sayontan Sinha