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.

dotnet add package Structr.AspNetCore.Validation

Setup

Setup validation services in your ASP.NET Core application:

services.AddAspNetCoreValidation();

Usage

Add validation attributes for your models. For example:

public class AddUserViewModel
{
    public string FullName { get; set; }
    public bool IsEmployee { get; set; }

    [RequiredIf(nameof(IsEmployee), true, ErrorMessage = "Employee must have some role.")]
    public IEnumerable<int> RolesIds { get; set; }

    public DateTime? ActiveFrom { get; set; }

    [GreaterThanOrEqualTo(nameof(ActiveFrom), PassOnNull = true)]
    public DateTime? ActiveTo { get; set; }
}

And check ModelState.IsValid property in controller action:

[HttpPost]
public IActionResult AddUser(AddUserViewModel addUserViewModel)
{
    if (ModelState.IsValid == false)
    {
        return View(addUserViewModel);
    }

    /* Some logic here */

    return RedirectToAction("Index");
}

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.

public class FooViewModel
{
    [RegularExpressionIf("[A-Z][a-z]\\d", nameof(Bar), true)]
    public string Foo { get; set; }

    public bool Bar { get; set; }
}

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:

<!DOCTYPE html>
<html>
<head>
    <!-- Some HTML here -->
    <!-- jQuery.Validation -->
    <script src="/assets/js/jquery.validation/jquery.validate.js"></script>
    <script src="/assets/js/jquery.validation/jquery.validate.unobtrusive.js"></script>
    <!-- AspNetCore.Validation -->
    <script src="/assets/js/aspnetcore.validation/aspnetcore.validation.js"></script>    
    <script src="/assets/js/aspnetcore.validation/aspnetcore.jquery.validation.js"></script>
    <script src="/assets/js/aspnetcore.validation/aspnetcore.validation.unobtrusive.js"></script>
</body>
</html>

Important: Order of includes client scripts is required!

Last updated