“MVC Foolproof Validation” is my first open source project to help bridge the gap in ASP.NET MVC Data Annotations when dealing with contingent or dependent scenarios
I like performing validation using Data Annotations in ASP.NET MVC. However, with complex scenarios, I find myself having to choose a different path. One complex scenario is “contingent scenario” also called “dependent scenario”:
Check out this simple class to demonstrate a contingent validation problem.
private class Person
{
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
[Required]
public bool Married { get; set; }
public string MaidenName { get; set; }
}
Let’s say our validation rule is: “If the user selects Married=true, then the user must also provide a MaidenName”.
Data Annotations, currently, don’t handle validating this rule. And, I can’t make a custom class-level annotation because that will only validate the model and not the individual item.
What I would like to write is:
[Required]
public bool Married { get; set; }
[RequiredIfTrue("Married")]
public string MaidenName { get; set; }
This simple yet unsupported markup fueled a couple of late night hack-a-thons. Now, I am pleased to announce the fruits of my labor… my first opensource project: “MVC Foolproof Validation”
http://foolproof.codeplex.com/
MVC Foolproof Validation extends the Data Annotation validation provided in ASP.NET MVC. Initial efforts are focused on contingent validation.
Some examples of what is possible:
public class SignUpViewModel
{
[Required]
public string Password { get; set; }
[EqualTo("Password", ErrorMessage="Passwords do not match.")]
public string RetypePassword { get; set; }
}
public class EventViewModel
{
[Required]
public string Name { get; set; }
[Required]
public DateTime Start { get; set; }
[Required]
[GreaterThan("Start")]
public DateTime End { get; set; }
}
Status of the project:
It’s in development, but it works.
Client validation is supported, both standard MVC and jQuery flavors.
Download the source, take a look and make suggestions. I feel strongly that Foolproof is something the community could really use. I hope to roll out a first release in a couple of weeks.

[...] to VoteFoolproof Provides Contingent Data Annotation Validation for ASP.NET MVC 2 (3/22/2010)Monday, March 22, 2010 from Nick RiggsI like preforming validation using Data Annotations in ASP.NET [...]
March 28, 2010 11:09 pm
nice post. thanks.
March 31, 2010 2:54 am
Maybe I’m missing something, but I can’t get this to work. I added the .dll file to the lib project in my solution, created a reference to it and still can’t get my class to recognize the attribute names listed.
May 13, 2010 1:54 pm
@William, what is the error message?
May 14, 2010 7:24 am
Hi,
Can I use foolproof in non MVC projects? (using buddy classes). When I try to use a foolproof attribute and try validation using Validator.TryValidateObject method, it results in a method or operation not implemented exception – from ModelAwareValidationAttribute class.
June 23, 2010 9:40 am
@Akmal, that doesn’t surprise me. The MVC framework is using the FoolproofValidator class to call a different IsValid() method than what’s in a standard ValidationAttribute.
So the short answer is No, it can’t run outside of Foolproof.
The long answer is yes – or probably rather. You would need to create a sub class of Validator that knows to call ModelAwareValidationAttribute’s IsValid(object, object) method.
June 23, 2010 4:20 pm
[...] Foolproof Provides Contingent Data Annotation Validation for ASP.NET MVC 2 [...]
July 23, 2010 7:39 pm
Very cool. Had to recompile in .NET 4.0 to use in my project. I was wondering if using this still makes sense in 4.0/MVC 3? or would you think the need for the Foolproof library is being replaced by standard MVC enhancement?
October 20, 2010 3:56 pm
Nando, check out my latest post on the subject: http://www.nickriggs.com/posts/asp-net-mvc-3-data-annotations-provide-property-level-contingent-validation/
October 20, 2010 5:23 pm