Search code examples
asp.net-corevalidationfluentvalidation

fluent validator in class library not work in asp.net core


when i put fluent validators in asp.net core client side validation project exactly work but when i put validator in class library not work

My model and validator in class library :

using FluentValidation;

namespace ClassLibrary1
{
    public class Person
    {
        public string Name { get; set; }
        public string Family { get; set; }
        public int Age { get; set; }
    }
    public class PersonValidator : AbstractValidator<Person>
    {
        public PersonValidator()
        {
            RuleFor(c => c.Name).NotEmpty().WithMessage("Name Is Empty");

        }
    }
}

In program.cs file :

services.AddFluentValidationAutoValidation(M =>
{
    M.DisableDataAnnotationsValidation = true;
}).AddFluentValidationClientsideAdapters()
  .AddValidatorsFromAssemblyContaining<PersonValidator>();

Solution

  • I found the solution.

    When the class library is nullable, the client-side validation in ASP.NET Core does not work.

    Solution:

    1. Remove <Nullable>enable</Nullable> from the *.csproj

    2. Define nullable property:

      public string? name{get;set}