I am localizing a site with fluent validation and resource files and everything is working fine, except that when I try to set up a custom error using a validation "When()"
condition.
In the following example:
public class InstructionValidator : AbstractValidator<InstructionModel>
{
public InstructionValidator()
{
RuleFor(x => x.Name)
.Length(0, 50)
.WithMessage(Resources.InvalidError);
RuleFor(x => x.Revision)
.NotNull()
.InclusiveBetween(1, int.MaxValue)
.WithMessage(Resources.InvalidError);
RuleFor(x => x.RevisionDate)
.NotNull()
.When(x => x.Revision > 1)
.WithMessage(Resources.RevisionDateRequiredError);
}
}
the first two rules (for Name and Revision) work fine and show either English or Spanish messages based on whatever culture the user has picked.
However, the third rule, with the "when"
condition, will always show the English message.
I have checked all the other rules in use on the site, and it is consistent - everyone works showing English or Spanish based on the culture set, except when a "when"
condition is used, then it's always English.
Maybe this has something to do with the "when"
being a server vs client check? I did try playing around with "WithLocalizedMessage()"
instead of "WithMessage()"
, and setting the ValidatorOptions.ResourceProviderType
, but still couldn't get it to work probably.