How to use FluentValidation.AspNetCore and FluentValidation.MVC6 to validate Entities in AspNetCore , can anyone give me an example ?
The FluentValidation. AspNetCore package provides auto-validation for ASP.NET Core MVC projects by plugging into ASP. NET's validation pipeline.
To run the validator, instantiate the validator object and call the Validate method, passing in the object to validate. Customer customer = new Customer(); CustomerValidator validator = new CustomerValidator(); ValidationResult result = validator. Validate(customer);
FluentValidation is a . NET library for building strongly-typed validation rules.
This is working for me:
project.json add:
"FluentValidation.AspNetCore": "6.4.0-beta3"
startup.cs
services
.AddMvc()
.AddFluentValidation(fv => fv.RegisterValidatorsFromAssemblyContaining<Startup>());
Validation:
public class Foo
{
public string Bar {get; set;}
}
public class FooValidator : AbstractValidator<Foo>
{
public FooValidator()
{
RuleFor(x => x.Bar).NotEmpty().WithMessage("Error Message");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With