Error message provider and Localization

Since version 1.0.1.0, NValidator has interface IErrorMessageProvider which allows you to implement custom error message source. The default instance for this interface is an ResourceErrorMessageProvider object which use the built-in resource file as the source of error messages. That means you can add your custom resource file, change the default value of IErrorMessageProvider at Bootstrapper or at application start using an instance of ResourceErrorMessageProvider.

public class ResourceErrorMessageProvider : IErrorMessageProvider
{
    private readonly ResourceManager _rm;

    public ResourceErrorMessageProvider(Type resourceType)
    {
        if (resourceType == null)
        {
            throw new ArgumentNullException("resourceType");
        }
        _rm = new ResourceManager(resourceType);
        
    }

    public string GetError(object key)
    {
        if (key == null || string.Empty.Equals(key))
        {
            throw new ArgumentException("Resource key can not be null or empty.");
        }
        return _rm.GetString(key.ToString());
    }
}

Given that you have created a resource file name MyResource. Here is the code to change the default resource file for your application in Bootstrapper or at application start:

ValidatorFactory.Config.DefaultErrorMessageProvider = new ResourceErrorMessageProvider(typeof(MyResource));


The NValidator built-in resource contains following key/value:
Name Value
NValidator_Validators_AllowedValuesValidator_GetErrors @PropertyName must be one of the allowed values. Allowed values: @AllowedValues.
NValidator_Validators_AllowedValuesValidator_GetNegatableErrors @PropertyName must not be one of the following values: @ProhibitValues.
NValidator_Validators_EqualValidator_Message @PropertyName must equal to @ComparisonValue.
NValidator_Validators_GreaterThanOrEqualValidator_Message @PropertyName must be greater than or equal @ComparisonValue.
NValidator_Validators_GreaterThanValidator_Message @PropertyName must be greater than @ComparisonValue.
NValidator_Validators_IsNullValidator_GetErrors @PropertyName must be null.
NValidator_Validators_IsNullValidator_GetNegatableErrors @PropertyName must not be null.
NValidator_Validators_LengthValidator_GetValidationResult @PropertyName must be between @MinimumLength and @MaximumLength in length. You had @TotalLength in length.
NValidator_Validators_LessThanOrEqualValidator_Message @PropertyName must be less than or equal @ComparisonValue.
NValidator_Validators_LessThanValidator_Message @PropertyName must be less than @ComparisonValue.
NValidator_Validators_NotEmptyValidator_GetValidationResult @PropertyName cannot be empty.
NValidator_Validators_NotEqualValidator_Message @PropertyName must not equal to @ComparisonValue.
NValidator_Validators_NotNullValidator_GetErrors @PropertyName must not be null.
NValidator_Validators_NotNullValidator_GetNegatableErrors @PropertyName must be null.
NValidator_Validators_PredicateValidator_GetValidationResult @PropertyName does not match condition.



If you don't want to use Resource file, you can implement IErrorMessageProvider and store your strings in SQL database, XML or whatever you want. However, there is one important note, the following keys must be defined in your data source to make default error messages work:
  • NValidator_Validators_AllowedValuesValidator_GetErrors
  • NValidator_Validators_AllowedValuesValidator_GetNegatableErrors
  • NValidator_Validators_EqualValidator_Message
  • NValidator_Validators_GreaterThanOrEqualValidator_Message
  • NValidator_Validators_GreaterThanValidator_Message
  • NValidator_Validators_IsNullValidator_GetErrors
  • NValidator_Validators_IsNullValidator_GetNegatableErrors
  • NValidator_Validators_LengthValidator_GetValidationResult
  • NValidator_Validators_LessThanOrEqualValidator_Message
  • NValidator_Validators_LessThanValidator_Message
  • NValidator_Validators_NotEmptyValidator_GetValidationResult
  • NValidator_Validators_NotEqualValidator_Message
  • NValidator_Validators_NotNullValidator_GetErrors
  • NValidator_Validators_NotNullValidator_GetNegatableErrors
  • NValidator_Validators_PredicateValidator_GetValidationResult

WithLocalizedMessage()

Use this method to assign custom error message to a validator with a messageKey. Basically, internally, it will use default error message provider to resolve the error message so in order to use this one, you have to have your custom resource defined as describe above.

Example:
RuleFor(person => person.Address.StreetLine)
             .Length(10, 50)
             .WithLocalizedMessage("Address_StreetLine_ErrorMessage_Key");

// *Address_StreetLine_ErrorMessage_Key* is the key value to fine your error message

Last edited Feb 7, 2012 at 3:31 AM by nvthoai, version 3

Comments

No comments yet.