Question

I have 2 properties Email and Confirm Email. How to provide string format arg for both localized property Name For Example:-

public class Account 
{
  public string Email {get;set;}
  public string ConfirmEmail {get;set;}
}

Public AccountValidator : AbstractValidator<Account>
{
   public AccountValidator()
   {
      RuleFor(a=> a.Email)
                  .Equal(a=>a.ConfirmEmail)
                  .WithLocalizedMessage(() => MyResource.compareFields);
    }  
}

My resource File has message like this:-

'{PropertyName}' and '{ ??? }' fields do not match.

What should I use in ??? so that it says : -

'Email' and 'ConfirmEmail' fields do not match.

Was it helpful?

Solution

One way to solve this to define a custom parameter in your message: {0}. So your the message would look like this in your resource file.

'{PropertyName}' and '{0}' fields do not match.

And pass in the "ConfirmEmail" as an extra argument to the WithLocalizedMessage call:

RuleFor(a => a.Email)
    .Equal(a => a.ConfirmEmail)
    .WithLocalizedMessage(() => MyResource.compareFields, "ConfirmEmail");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top