From 61f1e16d5aec63284e42a099fefb362a5d8c5ea1 Mon Sep 17 00:00:00 2001 From: Jason Finch Date: Tue, 4 Dec 2018 16:22:29 +1000 Subject: [PATCH] docs (Clone): Clone existing docs on codeplex into markup. --- README.md | 160 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 158 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a16ac2a..c0e8e2f 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,160 @@ -foolproof -========= +# foolproof MVC Foolproof Validation aims to extend the Data Annotation validation provided in ASP.NET MVC. + +## Install + +via NuGet: + +```powershell +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. + +## Setup + +Note: To build the source code, you will need to have the Microsoft AJAX Minifier installed: http://aspnet.codeplex.com/releases/view/40584 + +## Operator Validation + +```c# +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; } +} +``` + +## Included Operator Validators + +```c# +[Is] +[EqualTo] +[NotEqualTo] +[GreaterThan] +[LessThan] +[GreaterThanOrEqualTo] +[LessThanOrEqualTo] +``` + +## Required Validation + +```c# +private class Person +{ + [Required] + public string FirstName { get; set; } + + [Required] + public string LastName { get; set; } + + public bool Married { get; set; } + + [RequiredIfTrue("Married")] + public string MaidenName { get; set; } +} +``` + +## Included Required Validators + +```c# +[RequiredIf] +[RequiredIfNot] +[RequiredIfTrue] +[RequiredIfFalse] +[RequiredIfEmpty] +[RequiredIfNotEmpty] +[RequiredIfRegExMatch] +[RequiredIfNotRegExMatch] +``` + +## Client Validation + +To enable client validation, include MvcFoolproofValidation.js with the standard client validation script files: + +```html + + + + +``` + +## jQuery Validation + +If you are using jQuery validation, include MvcFoolproofJQueryValidation.js with the standard client validation script files: + +```html + + + + +``` + +## MVC 3 jQuery Unobtrusive Support + +```html + + + + +``` + +## Complex Custom Validation + +### Custom Validation Attribute: + +```c# +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 + +```c# +public class CreateUserViewModel +{ + [Required] + public string Username { get; set; } + + [Required] + public string Department { get; set; } + + [Required] + [RoleValidInDepartment(ErrorMessage="This role isn't valid for the selected department.")] + public string Role { get; set; } +} +``` + +[More information on building a custom validation attribute](http://nickriggs.com/posts/build-model-aware-custom-validation-attributes-in-asp-net-mvc-2/) \ No newline at end of file