Reputation: 712
I'm tring to build a validation rule using FluentValidation lib.
When I use the first part of the code bellows, it works as expected; but when I use the second part, only the first validation is executed, ignoring the last two validation rules.
The second part is not supposed to work? Am I doing something wrong?
// first part -- it works
RuleFor(c => c.Field).RequiredField().WithMessage(CustomMsg1);
RuleFor(c => c.Field).ValidateField().WithMessage(CustomMsg2);
RuleFor(c => c.Field).IsNumeric().WithMessage(CustomMsg3);
// second part -- validates only the first rule
RuleFor(c => c.Field)
.RequiredField().WithMessage(CustomMsg1)
.ValidateField().WithMessage(CustomMsg2)
.IsNumeric().WithMessage(CustomMsg3);
Upvotes: 0
Views: 3473
Reputation: 901
You can specify FluentValidator's cascading behavior with the CascadeMode enum.
You can read more about it here.
The default behavior should be the one you need, but if it works differently without setting the CascadeMode, maybe the default value is globally set somewhere in your project.
Configuring the default value globally looks like this:
ValidatorOptions.Global.CascadeMode = CascadeMode.Stop;
Your code with the default value set above should look something like this:
RuleFor(c => c.Field)
.Cascade(CascadeMode.Continue)
.RequiredField().WithMessage(CustomMsg1)
.ValidateField().WithMessage(CustomMsg2)
.IsNumeric().WithMessage(CustomMsg3);
Upvotes: 2