FluentValidation
FluentValidation copied to clipboard
[Bug] `SetValidator` appears to take no effect
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:
- Create an object with a list of T
- Create a validator for T, which a rule you can easily attempt to break
- 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
- attempt to submit and notice that validation passes, and OnValidSubmit triggers
- Recreate the same rules within ChildRules
- 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);
});