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:
Property name | Description |
---|---|
| Value of the related property with which the value of validating property will be compared. |
| Display name of the related property with which value the value of validating property will be compared. Will be used in error message if validation fails. |
| An error message to associate with a validation control if validation fails. |
| The error message resource name to use in order to look up the |
| The resource type to use for error-message lookup if validation fails. |
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.
Attribute name | Description |
---|---|
| Specifies that a data field value must be equal to a value of specified related property. |
| Specifies that a data field value must NOT be equal to a value of specified related property. |
| Specifies that a data field value must be greater than a value of specified related property. |
| Specifies that a data field value must be greater or equal to a value of specified related property. |
| Specifies that a data field value must be less than a value of specified related property. |
| Specifies that a data field value must be less or equal to a value of specified related property. |
| Specifies that a data field value must be contained in value of specified related property. In case of array-type of related property simple inclusion will be checked. If it's not an array-type the equality operator will be used. |
| Specifies that a data field value must NOT be contained in value of specified related property. In case of array-type of related property simple inclusion will be checked. If it's not an array-type the equality operator will be used. |
| The generalized version of those above. Specifies that a data field value must match a value of specified related property. This one needs a matching |
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.
Attribute name | Description |
---|---|
| Marks property as required when related property is empty. |
| Marks property as required when related property is NOT empty. |
| Marks property as required when related property equals |
| Marks property as required when related property equals |
| Marks property as required when related property matches provided regular expression. |
| Marks property as required when related property DOESN'T match provided regular expression. |
| The generalized version of attributes above. |
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:
Script name | Description |
---|---|
Basic client validation functions. | |
Adapters for jQuery validation plugin. | |
Adapters for unobtrusive. |
Important: Order of includes client scripts is required!
Last updated