Search code examples
asp.net-corefluentvalidation

How to use FluentValidation.AspNetCore and FluentValidation.MVC6?


How to use FluentValidation.AspNetCore and FluentValidation.MVC6 to validate Entities in AspNetCore , can anyone give me an example ?


Solution

  • 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");
        }
    }