Localization of error messages in ASP.NET MVC 2 (default validation attributes)

I work on a multilingual website and I want to localize validation error messages for most ValidationAttribute attributes such as [Requried]

I know that this can be done as Phil Haack showed in this article .

 [Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "Required")] 

but I want to configure the error message as I did with my validation attributes:

 [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)] public sealed class ValidateminRequiredNonalphanumericCharactersAttribute : ValidationAttribute { private const string _defaultErrorMessage = // Message From Resource Here ( i will be using two variables in this message ) private readonly int _minnonalphanumericCharactersCounter = Membership.Provider.MinRequiredNonAlphanumericCharacters; public ValidateminRequiredNonalphanumericCharactersAttribute() : base(_defaultErrorMessage) { } public override string FormatErrorMessage(string name) { return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString, name, _minnonalphanumericCharactersCounter); } public override bool IsValid(object value) { string valueAsString = value as string; if (String.IsNullOrEmpty(valueAsString)) return false; int nonalphanumericCharactersCounter = 0; char[] password = valueAsString.ToCharArray(); foreach (char c in password) { if (!char.IsNumber(c) && !char.IsLetter(c)) nonalphanumericCharactersCounter++; } return (nonalphanumericCharactersCounter >= _minnonalphanumericCharactersCounter); } } 

any idea?

+4
source share
1 answer

I understood how this is done. it is really simple and straightforward.

What I did was that I created my own RequiredAttribute . instead of using the built-in RequiredAttribute .

The only downside is that you will need to implement the logic of this validator yourself.

I know that some may consider redoing what is already there. (reinvent the wheel), but in this way I will have full control over the Validator logic and error message.

As you can see, the logic is implemented in the IsValid() method below.

Here is the RequiredAttribute class that I created:

 [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = true, Inherited = true)] public sealed class RequiredAttribute : ValidationAttribute { private const string _defaultErrorMessage = // Error Message // Notice that i can include the filed name in the error message // which will be provided in the FormatErrorMessage Method public RequiredAttribute() : base(_defaultErrorMessage) { } public override string FormatErrorMessage(string name) { return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString, name); } public override bool IsValid(object value) { if (value == null || String.IsNullOrWhiteSpace(Convert.ToString(value))) return false; else return true; } } 

Now, when it comes to using Validator, you will need to provide a full reference to your new class, as it will interfere with the standard built-in class System.ComponentModel.DataAnnotations.RequiredAttribute in my example above.

in my case it looks like this:

  [Amaly.Data.Validators.Required] public string Username { get; set; } 

Hope this was helpful.

+3
source

All Articles