This project is read-only.

NuGet: install-package foolproof

MVC Foolproof Validation aims to extend the Data Annotation validation provided in ASP.NET MVC. Initial efforts are focused on adding contingent validation.


Note: To build the source code, you will need to have the Microsoft AJAX Minifier installed:

Operator Validation

public class SignUpViewModel
    public string Password { get; set; }

    [EqualTo("Password", ErrorMessage="Passwords do not match.")]
    public string RetypePassword { get; set; }
public class EventViewModel
    public string Name { get; set; }

    public DateTime Start { get; set; }

    public DateTime End { get; set; }

Included Operator Validators


Required Validation

private class Person
    public string FirstName { get; set; }

    public string LastName { get; set; }

    public bool Married { get; set; }

    public string MaidenName { get; set; }

Included Required Validators


Client Validation

To enable client validation, include MvcFoolproofValidation.js with the standard client validation script files:

<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>
<script src="../../Scripts/MvcFoolproofValidation.js" type="text/javascript"></script>

jQuery Validation
If you are using jQuery validation, include MvcFoolproofJQueryValidation.js with the standard client validation script files:

<script src="../../Scripts/jquery.js" type="text/javascript"></script>
<script src="../../Scripts/jquery-validate.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcJQueryValidation.js" type="text/javascript"></script>
<script src="../../Scripts/MvcFoolproofJQueryValidation.js" type="text/javascript"></script>

MVC 3 jQuery Unobtrusive Support

<script src="@Url.Content("~/Scripts/jquery.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/mvcfoolproof.unobtrusive.js")" type="text/javascript"></script>

Complex Custom Validation

Custom Validation Attribute:

public class RoleValidInDepartmentAttribute : ModelAwareValidationAttribute
    //this is needed to register this attribute with foolproof's validator adapter
    static RoleValidInDepartmentAttribute() { Register.Attribute(typeof(RoleValidInDepartmentAttribute)); }

    public override bool IsValid(object value, object container)
        if (value != null && value.ToString() == "Software Developers")
            //if the role was software developers, we need to make sure the user is in the IT department
            var model = (CreateUserViewModel)container;
            return model.Department == "IT Department";

        //the user wasn't in a constrained role, so just return true
        return true;

Applied to a Model:

public class CreateUserViewModel
    public string Username { get; set; }

    public string Department { get; set; }

    [RoleValidInDepartment(ErrorMessage="This role isn't valid for the selected department.")]
    public string Role { get; set; }

More information on building a custom validation attribute


PS: My new year resolution was to take this porject out of it's 4 year hibernation status, after taking it over.

Last edited Jan 3, 2016 at 10:17 PM by Dromar, version 29