4

I use Data Annotations to validate my Web API 2 models. For basic attribute-based validation (Required, Range, etc.) it's pretty easy to provide localized messages by injecting custom ModelMetadataProvider. However, for more complex rules I implement IValidatableObject which returns a sequence of ValidationResult:

public class ValidationResult
{
    public ValidationResult(string errorMessage);
    public ValidationResult(string errorMessage, IEnumerable<string> memberNames);
}

It looks like there is no way to specify ErrorMessageResourceName here. And I do not want to make my models dependent on the localization provider. How can I solve the problem?

UserControl
  • 14,766
  • 20
  • 100
  • 187
  • I don't see a problem...Why don't you just pass a Localized string in the contructor.Or if you want to pass just the key use the current ResourceManager to find the actual value... – George Vovos Feb 02 '17 at 09:59

1 Answers1

2

It can happen if IValidatableObject.Validate method is called before the culture is available to the system. If the Validate method is called manually from the controller action, the error messages are properly localized.

Where you are setting the culture? you need to set it in Controller > ExecuteCore. Please have a look at this post or This post may help.

Community
  • 1
  • 1
Aqdas
  • 868
  • 4
  • 16
  • 57