Search code examples
asp.net-core-webapifluentvalidation

FluentValidation always return generic error message


I am using FluentValidation and I have the following validation rule:

      RuleFor(x => x.ClassId)
            .NotNull()
            .GreaterThan(0)
            .WithMessage("Class is required field");

but when ClassId is null it shows its generic error message:

'Class Id' must not be empty

How to show the error message I specified inside the WithMessage() method?


Solution

  • Depending on Md Farid Uddin Kiron's comment I think for every rule we need to add WithMessage() as followings:

          RuleFor(x => x.ClassId)
            .NotNull()
            .WithMessage("Class is required field")
            .GreaterThan(0)
            .WithMessage("Class is required field");