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:
http://foolproof.codeplex.com/
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:
http://dataannotationsextensions.org/
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:
http://git.mooncode.net/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!
Cheers,
@mkennedy
you should also review fluent validations
Thanks for the recommendation! Will check it out.
— Michael
if you need RequiredIf or similar attributes, then use this http://git.mooncode.net/moon.validation, since it’s stll a living project.
I also hope, they extend the library with a HideIf atribute according to my specification – this could help a lot when creating UI without the need to add jquery plumbing only to hide a div when a value changes… If you think this attribute is usefull, just ask the author of the moon.validator to include it in the next release :D
There is yet another library, created somewhere in early 2014 (so a few months after that post has been written). You can take a look in some spare time. It’s called ExpressiveAnnotations and is quite extensible: https://github.com/JaroslawWaliszko/ExpressiveAnnotations.
Very nice one, thanks for sharing!