Fluent Validation In ASP.NET MVC

Validation is a process to validate and check the data inserted by the user in the view. ASP.NET MVC provides various mechanisms for the validation like Remote Validation, Validation using Data Annotations, Fluent Validation and Custom Validation. In this article, we will read about Fluent Validation. Fluent Validation contains .NET libraries and the validation is performed using the Lambda expression. Use Fluent Validation when you want to create some advanced and complex validation for the user data. Let’s start this session.

First go to File-> New-> Project and create ASP.NET MVC project.

MVC

Now, we will load the required libraries for Fluent Validation. For this,  Right Click on the project name and click the “Manage NuGet Packages” option. Now, insert “FluentValidation” in the search box, select the FluentValidation libraries and install this.

install

Now, we will create an “Employee” Class in the model, as shown below:

Employee

After creating the Employee class, we will create another class (“Employee_Validation”) and insert all the validation rules in this class. Code of Employee_Validation class is shown below:

  1. using FluentValidation;  
  2. using System;  
  3.   
  4. namespace Fluent_Validation.Models  
  5. {  
  6.     public class Employee_Validation: AbstractValidator<Employee>  
  7.     {  
  8.         public Employee_Validation()  
  9.         {  
  10.             RuleFor(x => x.Name).NotEmpty().WithMessage("Employee Name is required");  
  11.             RuleFor(x => x.Mail_ID).NotEmpty().WithMessage("Employee Mail_ID is required");  
  12.             RuleFor(x => x.DOB).NotEmpty().WithMessage("Employee DOB is required");  
  13.             RuleFor(x => x.Password).NotEmpty().WithMessage("Employee Password is required");  
  14.             RuleFor(x => x.Confirm_Password).NotEmpty().WithMessage("Employee Password is required");  
  15.             RuleFor(x => x.Confirm_Password).Equal(x => x.Password).WithMessage("Password Don't Match");  
  16.   
  17.             RuleFor(x => x.Mail_ID).EmailAddress().WithMessage("Email Address is not correct");  
  18.             RuleFor(x => x.DOB).Must(Validate_Age).WithMessage("Age Must be 18 or Greater");  
  19.             RuleFor(x => x.Password).Must(PAssword_Length).WithMessage("Password Length must be equal   or greater than 8");  
  20.   
  21.   
  22.   
  23.         }  
  24.   
  25.         private bool PAssword_Length(string  pass_)  
  26.         {  
  27.              
  28.   
  29.             if (pass_.Length<8)  
  30.             {  
  31.                 return false;  
  32.             }  
  33.             else  
  34.             {  
  35.                 return true;  
  36.             }  
  37.         }  
  38.   
  39.         private bool Validate_Age(DateTime Age_)  
  40.         {  
  41.             DateTime Current = DateTime.Today;  
  42.             int age = Current.Year - Convert.ToDateTime(Age_).Year;  
  43.   
  44.             if (age < 18)  
  45.             {  
  46.                 return false;  
  47.             }  
  48.             else  
  49.             {  
  50.                 return true;  
  51.             }  
  52.         }  
  53.     }  
  54. }  
To implement Fluent Validation, we are required to inherit “AbstractValidator” class, which is a base class for Fluent Validation. Put all the required validation rules in Employee_Validation constructor. RuleFor method is used to define the validation rule for a class property and a property is accessed by Lambda expression. The various types of validation can be implemented on the properties like NotEmpty, comparison, Must, NotEqual and other types of the validations. After implementing all the required validation, we create a controller.

To add a controller, right click on Controller folder, create an empty Controller and name it “EmployeeController”.

EmployeeController

In this class, we will create an index action for HttpPost action selector. In this index action method, we will create the object of Employee_Validation class, pass the Student Model object into the Validate method and retrieve the result in ValidationResult object. Check for the error, if ValidationResult object contains any error, it will return the error list and this error list will be visible in the view section.

View

After creating the controller, we will add the view for the index action method.

add View

Now, check the view. You will find that all the fields are created for each property of Employee model.

property

Now, run the Application.

application
After running the Application, insert some data and click “create” button.

create

When we submit the above form, the following errors will generate:
errors

If we insert the valid data, no error will generate.

valid data
Conclusion

Use Fluent Validation if you want to implement complex or high level validation. For Normal Validation, you can prefer Data Annotation Validation. 

Up Next
    Ebook Download
    View all
    Learn
    View all