3

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.

Fateme Mirjalili
  • 762
  • 7
  • 16
TomZomW
  • 531
  • 1
  • 5
  • 15

2 Answers2

2

After some more testing, I found out what was going on. There was no issue with the resources or the validation syntax, it was the order the localization was occurring at.

I have [Localize] filter applied to a base controller, and it worked for everything, except server side validation, which must be occur before the filter is triggered.

While I'm still not sure how to redo the localization to deal with this issue, the original question I asked has been solved--moving the culture set inside of the validator does indeed get the proper error messages to show.

TomZomW
  • 531
  • 1
  • 5
  • 15
  • 1
    I used the answer from this post to fix my problem. http://stackoverflow.com/questions/8226514/best-place-to-set-currentculture-for-multilingual-asp-net-mvc-web-applications – TomZomW Oct 19 '15 at 21:30
1

Are you sure that you have Resources.RevisionDateRequiredError on your ResourceManager.es?

It works fine for me. However, if you have it in there I can suggest a different solution for you using Must().

public InstructionValidator()
{
    // ...
    RuleFor(x => x.RevisionDate)
        .NotNull()
        .Must((instruction, revisionDate) => IsRevisionDate(instruction.Revision, revisionDate))
        .WithMessage(Resources.RevisionDateRequiredError);
}

And then, create a method that returns bool called "IsRevisionDate":

private bool IsRevisionDate(int revision, DateTime revisionDate)
{
    return revision > 1;
}

I hope it helps you.

Fateme Mirjalili
  • 762
  • 7
  • 16
  • It's definitely not a missing resource. I substituted the resource in question into other validation rules and it worked fine. I tried your example, and while it didn't work as is (the NotNull() before the Must() made the field always required), I did look up the Must() documentation which might be helpfull here with a bool method that checks revision and revisiondate. But the problem with that is that the project this example is taken from has a large amount of when validation conditions that would then all need to be redone, which I'm hoping to avoid. – TomZomW Oct 14 '15 at 21:22
  • Well, I suggest you to take off the NotNull() and see if the "When()" is gonna work. Besides you can add a "Cascade" to your Rule as well. Something like this: `RuleFor(p => p.RevisionDate).Cascade(CascadeMode.Continue).NotNull().When(x => x.Revision > 1);` – Alison Henrique Jonck Oct 15 '15 at 11:07
  • Or this too: `RuleFor(p => p.RevisionDate).When(x => x.Revision > 1, () => { RuleFor(p => p.RevisionDate).NotNull(); });` – Alison Henrique Jonck Oct 15 '15 at 11:16
  • Thanks for the new ideas aj. I had already tried changing the order and the cascade, but whenever the When() was used, regardless of where it appeared, it caused the issue still. – TomZomW Oct 15 '15 at 20:40