The goal of this post is to show you validation options beyond those that come in-the-box from Microsoft and the ASP.NET team.
Currently, there are nine pre-built validation attributes that come with MVC:
While these are very helpful, knowing where to look to find additional validation options is super helpful. For example, what if the State property is only supposed to be required if Country property is set to USA? Good luck with that one!
So in this post, I will point you at a few open-source DataAnnotation libraries built for MVC.
The first to check is FoolProof:
Jump over there and browse through the various attributes available. There are a bunch, such as RequiredIfTrue. Like so many things with ASP.NET MVC, the way to add foolproof to your project is to use NuGet. Just fire it up and search for FoolProof:
Foolproof is great for dependent validation scenarios such as this field is required if that field is checked or something like that.
Next up, DataAnnotationsExtensions:
DataAnnotationsExtensions is a nice one as well. This library is focused more on validating certain data types: credit cards, dates, urls, etc. Again, it’s available from NuGet. This time you probably want to reference DataAnnotationsExtensions.MVC3 rather than the base project.
Finally, let’s look at Moon.Validation:
Look at the files there that end in “Attribute”. Again, there are some nice, dependent validation attributes here.
I hope that gives you a sense of what’s out there beyond just the built-in validation options. I’m sure there are a bunch of projects that I missed. If you know of any, add a comment with a link and why you like it!