FluentValidation icon indicating copy to clipboard operation
FluentValidation copied to clipboard

[Bug] `SetValidator` appears to take no effect

Open pingu2k4 opened this issue 1 year ago • 0 comments

Describe the bug When using SetValidator on a list of items, it appears to not be used. Whereas replicating the same rules within ChildRules does work.

To Reproduce Steps to reproduce the behavior:

  1. Create an object with a list of T
  2. Create a validator for T, which a rule you can easily attempt to break
  3. Create a form using the object with a list of T, and hook up the validation as well as the ability to create items and "break" the rule
  4. attempt to submit and notice that validation passes, and OnValidSubmit triggers
  5. Recreate the same rules within ChildRules
  6. Try again and notice that OnValidSubmit does not trigger.

Expected behavior Identical rules within ChildRules and SetValidator should behave identically

Screenshots If applicable, add screenshots to help explain your problem.

Hosting Model (is this issue happening with a certain hosting model?):

  • Blazor WebAssembly

Additional context I am using the following rules. The ModifyEmployeeModelRoleValidator has rules identical to those inside ChildRules. If I comment out the ChildRules part, then validation succeeds, incorrectly.

RuleForEach(x => x.Roles).SetValidator(new ModifyEmployeeModelRoleValidator());
RuleForEach(x => x.Roles).ChildRules(role =>
{
    role.RuleFor(x => x.Level).GreaterThanOrEqualTo(x => x.MinLevel)
        .When(x => x.MinLevel is not null, ApplyConditionTo.CurrentValidator)
        .LessThanOrEqualTo(x => x.MaxLevel - 1)
        .When(x => x.MaxLevel is not null, ApplyConditionTo.CurrentValidator);
});

pingu2k4 avatar Feb 20 '24 11:02 pingu2k4