Starting from the migration to asp.net core of the MVC Foolproof Validation library.
Validators to compare with other property:
Is(base class)EqualToNotEqualToGreaterThanLessThanGreaterThanOrEqualToLessThanOrEqualToIn(field must be in dependant)NotIn(field must not be in dependant)
Validators to compare with specific values:
Is<T>(base class)EqualTo<T>NotEqualTo<T>GreaterThan<T>LessThan<T>GreaterThanOrEqualTo<T>LessThanOrEqualTo<T>In<T>NotIn<T>IsEmptyIsTrueIsFalse
Improved required validators:
RequiredIf(field required if dependant satisfy condition)RequiredIfNot(field required if dependant doesn't satisfy condition)RequiredIfTrue(field required if dependant value is true)RequiredIfFalse(field required if dependant value is false)RequiredIfEmpty(field required if dependant has no value)RequiredIfNotEmpty(field required if dependant has value)RequiredIfRegExMatch(field required if dependant match regex)RequiredIfNotRegExMatch(field required if dependant doesn't match regex)RegularExpressionIf(field must match regex if dependant satisfy condition)
Predicate validators: Predicate validators will allow you to create complex validation rules (logical predicates), combining other validators (inlcuding the predicate ones).
Predicate(base abstract class)Not(operand)And(operand1, operand2,..., operandN)Or(operand1, operand2,..., operandN)IsValid(property, operand)
operand could be any ValidationAttribute, inlcuding the predicate ones, so you can recursively combine validators
to build any logical predicate.
IsValid will let you validate any property in the model, using any existing ValidationAttribute; so combining the
predicate validators and the IsValid validator, you can create validation rules to be evaluated at the model
level (aka model-wise validation).
All the validators are available for client side validation to use with jquery.validation or aspnet-client-validation.
Although you can combine any ValidationAttribute using the predicate validators, only operands (ValidationAttribute) with
a registered IAttributeAdapter or implementing the IClientModelValidator interface will be available for client-side validation; of course,
this include all the validators provided by this library (and many others from System.ComponentModel.DataAnnotations).
This library provides you with an HTML helper method ModelValidation to render the model-wise validation as you would do with any
property validation. To bring this helper in context, you need to include the FoolProof.Core namespace with the @using directive.
Take a look at the predicate page in the example app.
NuGet: install-package FoolProof.Core
- Include namespace FoolProof.Core
- Just add this line
services.AddFoolProof();to your ConfigureServices method on the Startup class; this will register a newIValidationAttributeAdapterProvider.
After installing the nuget package, a new folder foolproof-validation should be created under your wwwroot\lib folder
with all the required JavaScript files for the client-side validation. The content of this new folder correspond
with the content of the Scripts folder in the nuget package.
To integrate with jquery.validation, include the following JavaScript files in the given order:
jquery.js(NPM)jquery.validate.js(NPM)jquery.validate.unobtrusive.js(NPM)foolproof.core.jsfoolproof.validators.jsfoolproof.jquery.validation.jsfoolproof.jquery.validation.unobtrusive.js
To integrate with aspnet-client-validation, include the following JavaScript files in the given order:
aspnet-validation.js(NPM)foolproof.core.jsfoolproof.validators.jsfoolproof.aspnet-validation.jsfoolproof.aspnet-validation.unobtrusive.js
Note: Once the page get loaded, FoolProofCore.aspnetValidationService will contain a bootstrapped instance of a ValidationService, with all
the available validation methods already registered and associated with the corresponding form fields.
You can review a kind of DEMO app (the WebApp used to execute the E2E tests) here: E2E/Demo WebApp