19

There's a way to set the default resource to the data annotations validations?

I don't wanna make something like this:

[Required(ErrorMessage="Name required.", ErrorMessageResourceType=typeof(CustomDataAnnotationsResources)]
public string Name { get; set; }

I would like something like this:

Global.asax

DataAnnotations.DefaultResources = typeof(CustomDataAnnotationsResources);

then

[Required]
public string Name { get; set; }

someone gimme a light!

thanks in advance

EDIT

My real problem was with EF Code First CTP4. CTP5 fix it. Thanks for everyone.

Kim Tranjan
  • 4,521
  • 3
  • 39
  • 38
  • Can somebody please shed light as to whether this is possible and how to approach this. Thanks. – Robin Maben Nov 29 '10 at 05:34
  • have a look at http://adamyan.blogspot.be/2010/02/aspnet-mvc-2-localization-complete.html you can find the list of default messages here: http://stackoverflow.com/a/24186038/187650 – juFo Jun 12 '14 at 13:45

3 Answers3

13

You could try doing this:

Add this class somewhere in your project:

 public class ExternalResourceDataAnnotationsValidator : DataAnnotationsModelValidator<ValidationAttribute>
{
    /// <summary>
    /// The type of the resource which holds the error messqages
    /// </summary>
    public static Type ResourceType { get; set; }

    /// <summary>
    /// Function to get the ErrorMessageResourceName from the Attribute
    /// </summary>
    public static Func<ValidationAttribute, string> ResourceNameFunc 
    {
        get { return _resourceNameFunc; }
        set { _resourceNameFunc = value; }
    }
    private static Func<ValidationAttribute, string> _resourceNameFunc = attr => attr.GetType().Name;

    public ExternalResourceDataAnnotationsValidator(ModelMetadata metadata, ControllerContext context, ValidationAttribute attribute)
        : base(metadata, context, attribute)
    {
        if (Attribute.ErrorMessageResourceType == null)
        {
            this.Attribute.ErrorMessageResourceType = ResourceType;
        }

        if (Attribute.ErrorMessageResourceName == null)
        {
            this.Attribute.ErrorMessageResourceName = ResourceNameFunc(this.Attribute);
        }
    }
}

and in your global.asax, add the following:

// Add once
ExternalResourceDataAnnotationsValidator.ResourceType = typeof(CustomDataAnnotationsResources);

// Add one line for every attribute you want their ErrorMessageResourceType replaced.
DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(RangeAttribute), typeof(ExternalResourceDataAnnotationsValidator));

It will look for a property with the same name as the validator type for the error message. You can change that via the ResourceNameFunc property.

EDIT: AFAIK this works from MVC2 onwards, as DataAnnotationsModelValidatorProvider was introduced in MVC2.

CGK
  • 2,662
  • 21
  • 24
  • @CGK: Please also take a look at this question.http://stackoverflow.com/questions/4301218/decorate-model-fields-with-metadata-from-resource-file. I have explained my requirements there although I have placed a bounty on this question. – Robin Maben Dec 03 '10 at 07:44
  • @conqenator: Answered there too. – CGK Dec 03 '10 at 12:07
  • How to use this solution for changing the default message: "The value '{0}' is not valid for {1}.", raised when the data the user entered isn't compatible with the data type. This is default validation - there is no need for explicit adding any attribute for data type validation, so how to localize the default error message ? – jwaliszko Feb 23 '11 at 14:13
  • This solution does not cover that problem. You can look at the following link for instructions on how to do that: http://stackoverflow.com/q/2480557/525499 – CGK Feb 23 '11 at 15:19
  • @CGK: I've seen that but the only problem is that I have my resources in separate assembly: http://stackoverflow.com/questions/5090751/changing-the-default-modelstate-error-messages-in-asp-mvc3 – jwaliszko Feb 23 '11 at 15:39
  • AFAIK that doesn't work with resources on a separate assembly. – CGK Feb 23 '11 at 15:57
  • I thought this solution was working, but it completely breaks client side validation, so I do not recommend it. Instead, it is possible to go [along these lines](http://stackoverflow.com/a/14419808/1946412) and override the default `DataAnnotationsModelValidatorProvider`. I did it and intercepted the attributes very easily, and I don't have to register the adapter for each validation attribute anymore. It's still _possible_ to use this solution, thought you will have to inherit each default adapter instead of the base adapter class, to maintain client side validation working. – julealgon Aug 28 '14 at 14:17
  • @julealgon Great addition. The answer provided handled cases before MVC provided client side validation if I remember correctly. – CGK Aug 29 '14 at 00:29
12

To achieve this, I created a new class that inherits from RequiredAttribute, and the error message is embedded inside this new class:

public class RequiredWithMessageAttribute : RequiredAttribute
{
    public RequiredWithMessageAttribute()
    {
        ErrorMessageResourceType = typeof(ValidationResource);
        ErrorMessageResourceName = "RequiredErrorMessage";
    }
}

The error message is taken from the ValidationResource.resx file, where I list the error message as follows:

RequiredErrorMessage --> "{0} is required."

where {0} = display name.

I then annotate my models like this, so I never have to repeat my error message declarations:

[RequiredWithMessage]
public string Name { get; set; }

Once you do this, an error message ("Name is required.") will appear whenever validation fails.

This works properly with ASP.NET MVC's server-side validation and client-side validation.

Johnny Oshika
  • 54,741
  • 40
  • 181
  • 275
  • It doesn't work!!! This method works for "DisplayNameAttribute" for example but it doesn't work for "RequiredAttribute". That drives me crazy :( I don't know what to do. – Amir Karimi Jan 20 '11 at 15:21
  • It works only on server side because it dont generate data-val-required html attribute in MVC5 – Pascal Carmoni May 07 '16 at 12:22
3

I did another approach. It still needs you to inherit DataAnnotation attributes, but you can get a more flexible translation solution.

Code from my blog post (read it fore more details)

End result

public class User
{
    [Required]
    [LocalizedDisplayNameAttribute("User_Id")]
    public int Id { get; set; }

    [Required]
    [StringLength(40)]
    [LocalizedDisplayNameAttribute("User_FirstName")]
    public string FirstName { get; set; }

    [Required]
    [StringLength(40)]
    [LocalizedDisplayNameAttribute("User_LastName")]
    public string LastName { get; set; }
}

1 Inherit all data annotation attributes like this

public class RequiredAttribute : System.ComponentModel.DataAnnotations.RequiredAttribute
{
    private string _displayName;

    public RequiredAttribute()
    {
        ErrorMessageResourceName = "Validation_Required";
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        _displayName = validationContext.DisplayName;
        return base.IsValid(value, validationContext);
    }

    public override string FormatErrorMessage(string name)
    {
        var msg = LanguageService.Instance.Translate(ErrorMessageResourceName);
        return string.Format(msg, _displayName);
    }
}

2 Inherit DisplayNameAttribute

public class LocalizedDisplayNameAttribute : DisplayNameAttribute
{
    private PropertyInfo _nameProperty;
    private Type _resourceType;

    public LocalizedDisplayNameAttribute(string className, string propertyName)
        : base(className + (propertyName == null ? "_Class" : ("_" + propertyName)))
    {

    }

    public override string DisplayName
    {
        get
        {
            return LanguageService.Instance.Translate(base.DisplayName) ?? "**" + base.DisplayName + "**";
        }
    }
}

3. Create the language service (you can switch to any language source in it)

public class LanguageService
{
    private static LanguageService _instance = new LanguageService();
    private List<ResourceManager> _resourceManagers = new List<ResourceManager>();

    private LanguageService()
    {
    }

    public static LanguageService Instance { get { return _instance; } }

    public void Add(ResourceManager mgr)
    {
        _resourceManagers.Add(mgr);
    }

    public string Translate(string key)
    {
        foreach (var item in _resourceManagers)
        {
            var value = item.GetString(key);
            if (value != null)
                return value;
        }

        return null;
    }
}

Finally you need to register the string tables you use to translate the validation messages and your models

LanguageService.Instance.Add(MyNameSpace.ModelResource.ResourceManager);
LanguageService.Instance.Add(MyNameSpace.ValidationResources.ResourceManager);
jgauffin
  • 99,844
  • 45
  • 235
  • 372
  • +1 for sharing all that code. I have yet to figure out quite how to use it (LoL) but it seems it will solve my problem – ekkis Jun 03 '11 at 03:10