0

I'm using FluentValidation version: 8.5.1 in my Asp.Net Core 3.0 Razor Pages project.

I have a class derived from base class as shown below:

public class BranchEditViewModel : BranchViewModel
{
    public Guid Id { get; set; }
    public bool Active { get; set; }
}

I'm trying to validate my derived class in my update method as shown below:

public async Task<IActionResult> OnPutAsync([FromForm] BranchEditViewModel model)

But this doesn't work for some reason. The Validations are not getting called.

When I create AbstractValidator for my base class as shown below, the validator is not getting called.

public class BranchViewModelValidator : AbstractValidator<BranchViewModel>

But if I create AbstractValidator for my dervied class as shown below, the validator is getting called.

public class BranchViewModelValidator : AbstractValidator<BranchEditViewModel>

Please can you assist on why this is not working with base class? Am I doing something wrong?

fingers10
  • 6,675
  • 10
  • 49
  • 87
  • 1
    The Base validator needs to be invoked manually. See https://stackoverflow.com/a/36022690/10091607 and https://github.com/JeremySkinner/FluentValidation/issues/738 – itminus Nov 07 '19 at 10:24

1 Answers1

0

Here is how I made this working. I created a separate BranchEditViewModelValidator and then called Include(new BranchViewModelValidator()) from it.

public class BranchEditViewModelValidator : AbstractValidator<BranchEditViewModel>
{
    public BranchEditViewModelValidator()
    {
        Include(new BranchViewModelValidator());
    }
}
fingers10
  • 6,675
  • 10
  • 49
  • 87