In my mvc5 web application, for localization service. I want to display the viewmodel property display name for different languages. In another issued, I found the way by override the default config in fluentvalidation .
so I wirte the follow code :
```
public static IRuleBuilderOptions<T, TProperty> GetErrorMessageResource<T, TProperty>(this IRuleBuilderOptions<T, TProperty> rule, string text)
{
return rule.Configure(config =>
{
config.CurrentValidator.ErrorMessageSource = new LocalizationValidator(text);
});
}
public static IRuleBuilderOptions<T, TProperty> GetDisplayNameResource<T, TProperty>(this IRuleBuilderOptions<T, TProperty> rule, string text)
{
return rule.Configure(config =>
{
config.DisplayName = new LocalizationValidator(text);
});
}
```
The view model code:
```
[Validator(typeof(PersonDemoValidator))]
public class PersonDemo
{
public string Email { get; set; }
public string Password { get; set; }
}
public class PersonDemoValidator : AbstractValidator<PersonDemo>
{
public PersonDemoValidator()
{
RuleFor(t => t.Email)
.NotEmpty()
.GetErrorMessageResource("UI_Customer_Validator_NotEmptyError")
.EmailAddress()
.GetErrorMessageResource("UI_Customer_Validator_EmailAddressError")
.GetDisplayNameResource("UI_Customer_Demo_Demo2_Email");
RuleFor(t => t.Password)
.Length(6, 10)
.GetErrorMessageResource("UI_Customer_Validator_PasswordLengthError")
.GetDisplayNameResource("UI_Customer_Demo_Demo2_Password");
}
}
```
When I start the application, I found that , the ErrorMessage can run correctly but the DisplayName can not be correctly. It still show the default property name as : Email , Password.
Can you give me some suggest to resolve it ? thanks very much !
so I wirte the follow code :
```
public static IRuleBuilderOptions<T, TProperty> GetErrorMessageResource<T, TProperty>(this IRuleBuilderOptions<T, TProperty> rule, string text)
{
return rule.Configure(config =>
{
config.CurrentValidator.ErrorMessageSource = new LocalizationValidator(text);
});
}
public static IRuleBuilderOptions<T, TProperty> GetDisplayNameResource<T, TProperty>(this IRuleBuilderOptions<T, TProperty> rule, string text)
{
return rule.Configure(config =>
{
config.DisplayName = new LocalizationValidator(text);
});
}
```
The view model code:
```
[Validator(typeof(PersonDemoValidator))]
public class PersonDemo
{
public string Email { get; set; }
public string Password { get; set; }
}
public class PersonDemoValidator : AbstractValidator<PersonDemo>
{
public PersonDemoValidator()
{
RuleFor(t => t.Email)
.NotEmpty()
.GetErrorMessageResource("UI_Customer_Validator_NotEmptyError")
.EmailAddress()
.GetErrorMessageResource("UI_Customer_Validator_EmailAddressError")
.GetDisplayNameResource("UI_Customer_Demo_Demo2_Email");
RuleFor(t => t.Password)
.Length(6, 10)
.GetErrorMessageResource("UI_Customer_Validator_PasswordLengthError")
.GetDisplayNameResource("UI_Customer_Demo_Demo2_Password");
}
}
```
When I start the application, I found that , the ErrorMessage can run correctly but the DisplayName can not be correctly. It still show the default property name as : Email , Password.
Can you give me some suggest to resolve it ? thanks very much !