Validation
Structr.AspNetCore.Validation package contains number of attributes supplying a wide range of developer's basic needs in validating user-input data in ASP.NET Core application. Everything is done in classic manner by specifying validation attributes for properties to be validated. Every attribute generates it's own but fully customizable error message. More to say - validation attributes could be combined between each other when you need it.
Inspiration for creating this package was found in validation package for ASP.NET MVC - Foolproof and ASP.NET Core fork - Foolproof.Core.
Installation
AspNetCore.Validation package is available on NuGet.
Setup
Setup validation services in your ASP.NET Core application:
Usage
Add validation attributes for your models. For example:
And check ModelState.IsValid
property in controller action:
List of properties that are available in each validation attribute:
So all attributes allow to specify custom error messages and get them from resource files if needed.
Simple validation
These attributes allow to specify related property which value will be used to check value of marked property.
In addition to properties available for all validation attributes, attributes of this type has PassOnNull
. This property indicates that validation should be passed if value of property to be validated or value of related property equals null
. In case of both values are null
then the behavior of validation will depend on type of attribute. GreaterThan
, LessThan
, NotIn
and NotEqualTo
will fail validation. Others will succeed.
Requirements
These allow to make marked property requirement as conditional and based on related property value.
Regular expressions
The last one is RegularExpressionIf
attribute which allows to check that a data field value must match the specified regular expression but only when related property has specified value.
This one will validate Foo
property to match [A-Z][a-z]\d
expression only if Bar
property will be true
.
Client
Use AspNetCore.Validation JavaScript files on client side with jQuery validation plugin:
Client scripts:
Important: Order of includes client scripts is required!
Last updated