I successfully plugged FluentValidation in a simple Blazor application and everything works fine for basic validation.
Imagine we have this classes
public class Person
{
public string Name { get; set; }
}
public class PersonValidator : AbstractValidator<Person>
{
PersonValidator()
{
RuleFor(p => p.Name).NotEmpty();
}
}
When I submit my form FluentValidation is doing it's job if the Name is empty.
My problem is that I have later another rule in my code for exemple Name must be unique in a database.
protected async Task HandleValidSubmit()
{
try
{
SavePersonInDb(Person);
}
catch (NotUniqueException e)
{
// How to add a validation error to the Name property ?
}
}
I'm not sure how to add an error to the ModelState and return to the form page like in Razor Pages or traditional MVC.