Use FluentValidation in MVC

Rebai Hamida
4 min readOct 23, 2019

--

Introduction

FluentValidation is your simple way to add validation in your web MVC application, you can customize your validators. In this article, I will work using .NET Core 3 so netstandard2.0 library but you can use it for older projects in net45 library.

Integration and configuration

To add it you can use NuGet console or NuGet Package Manager:

Install-Package FluentValidation

After we need to configure it, so, we need to add Fluent validation in the Startup class, inside ConfigurationServices method.

Create your first validator

We add a new model EmployeeModel:

namespace FluentValidationProject.Models
{
public class EmployeeModel
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public int Age { get; set; }
}
}

Now we will add a new directory called Validators where we will add all validators used in this application.

We will add a new class called EmployeeValidator that will iherit from AbstractValidator<EmployeeModel> and we will add all basic rules like not being nullable or a range or Email format verification …

public class EmployeeValidator : AbstractValidator<EmployeeModel>
{
public EmployeeValidator()
{
RuleFor(x => x.Id).NotNull();
RuleFor(x => x.Name).Length(0, 10);
RuleFor(x => x.Email).EmailAddress();
RuleFor(x => x.Age).InclusiveBetween(18, 60);
}
}

This class should be configured in the Startup class:

services.AddTransient<IValidator<EmployeeModel>, EmployeeValidator>();

Now, we will test our model in a view, so we will add form with some input according to our model and a submit button:

<div asp-validation-summary=”ModelOnly”></div>

<form asp-action=”Create”>
Id: <input asp-for=”Id” /> <span asp-validation-for=”Id”></span>
<br />
Name: <input asp-for=”Name” /> <span asp-validation-for=”Name”></span>
<br />
Email: <input asp-for=”Email” /> <span asp-validation-for=”Email”></span>
<br />
Age: <input asp-for=”Age” /> <span asp-validation-for=”Age”></span>
<br /><br />

<input type=”submit” value=”submit” />
</form>

And in the Controller we will add our method that can be Create method:

[HttpPost]
public IActionResult Create(EmployeeModel employee)
{

if (!ModelState.IsValid)
{ // re-render the view when validation failed.
return View(“Index”, employee);
}

return RedirectToAction(“Index”);

}

And we start the application from Visual Studio, we click on submit button to see the validation message in every textbox input in the form.

And if you like to have all errors messages to display them in the top of the view for example you have to instantiate the validator object and call the Validate method, passing in the object of your model to validate.

var validator = new EmployeeValidator();
ValidationResult results = validator.Validate(employee);
string allMessages = results.ToString(“-”);

In this case, each message will be separated with a -

The Validate method returns a ValidationResult object that include two properties:

  • IsValid : boolean to define if the validation suceeded.
  • Errors - a collection list of ValidationFailure objects that include all the details related to failures validation .

This is an exemple:

ValidationResult results = validator.Validate(employee);
var errors = new List<string>();
if (!results.IsValid)
{
foreach (var failure in results.Errors)
{
var error = $”Property {failure.PropertyName} failed validation. Error was: {failure.ErrorMessage}”;
errors.Add(error);
}
}

Complex validation

If you have complex validation that require a specific validator to the property, we will add Address in the model to apply a specific validation, so we create a new Validator:

public class AddressValidator : AbstractValidator<string>
{
}

After we come back to EmployeeValidator and we and the rule validation for Address property:

RuleFor(x => x.Address).SetValidator(new AddressValidator());

And we can add dependency validation between validation, so if a property is not nullable another property should be not nullable .

RuleFor(x => x.Age).NotNull().When(x => x.Address != null);

Collections

You can use the RuleForEach method to apply the same rule to many items in a collection, in our example we will add a list of string to define many address for an EmployeeModel:

public List<string> AddressLines { get; set; } = new List<string>();

Now, we will apply the same validation to all string added in this list, go back to your validator:

RuleForEach(x => x.AddressLines).NotNull(); RuleForEach(x => x.AddressLines).NotNull()
.Must(x => x.Length <= 10).WithMessage(“No more than 10 addresses are allowed”);

Customize error messages

You can customize your message to not receive the default error message for a validator by calling the WithMessage method on a validator definition:

RuleFor(x => x.Name).Length(0, 10).WithMessage(“Please ensure that you have entered your name”);

This is an overview about FluentValidation.

--

--

Rebai Hamida
Rebai Hamida

Written by Rebai Hamida

Senior Cloud Application Architect, Microsoft MVP in Developer Technologies, MCT, Technical writer, Speaker

No responses yet