13

Using a resx file in the App_GlobalResources directory, I've been able to change the default message for the PropertyValueInvalid string of the model validators.

But it doesn't work to translate the message when a value is required (PropertyValueRequired.)

In the Global.asax.cs Application_Start() I've changed the resource class key, like this:

DefaultModelBinder.ResourceClassKey = "Messages";

And in the Messages.resx files I've put two entries:

  • "PropertyValueInvalid" => "O valor '{0}' é inválido para o campo {1}."
  • "PropertyValueRequired" = > "É necessário digitar o {0}."

Thanks.

5
  • I've noticed the same behaviour. Solution anyone? Commented Mar 3, 2011 at 9:35
  • @mare What is your exact problem - PropertyValueRequired message is not being used from resx file in modal validation using data annotation? Commented Mar 3, 2011 at 19:22
  • Yes, exactly as daniel's issue. Commented Mar 4, 2011 at 23:19
  • have u tried my below solution? Commented Mar 9, 2011 at 10:32
  • I think Darin's is a bit better , it's an upgrade of your solution. Commented Mar 10, 2011 at 21:05

4 Answers 4

17

RequiredAttribute not used DefaultModelBinder.GetValueRequiredResource. Create custom DataAnnotationsModelValidator class.

public class MyRequiredAttributeAdapter : RequiredAttributeAdapter
{
  public MyRequiredAttributeAdapter(ModelMetadata metadata, 
                                    ControllerContext context, 
                                    RequiredAttribute attribute) 
         : base(metadata, context, attribute)
  {
    attribute.ErrorMessageResourceType = typeof (Messages);
    attribute.ErrorMessageResourceName = "PropertyValueRequired";
  }
}

and register adapter in Global.asax.

DataAnnotationsModelValidatorProvider.RegisterAdapter(
    typeof(RequiredAttribute), 
    typeof(MyRequiredAttributeAdapter));

Hope this help!

Reusable Validation Error Message Resource Strings for DataAnnotations

Sign up to request clarification or add additional context in comments.

8 Comments

+1 This is exactly what I have been looking for, thanks! (ps: the link is broken)
this is the best answer i've found on this subject
This is great. Please also see this for a solution to a trouble you might run into stackoverflow.com/a/16211476/268091
Where is a good best practice place to store the DataAnnotationsModelValidator class?
also what namespaces (using) does this require?
|
9
+25

This message is baked into System.ComponentModel.DataAnnotations assembly under the key RequiredAttribute_ValidationError. You could write a custom validation attribute:

public class MyRequiredAttribute : RequiredAttribute
{
    public MyRequiredAttribute()
    {
        ErrorMessageResourceType = typeof(Messages);
        ErrorMessageResourceName = "Required";
    }
}

and then inside your Messages.resx file define the Required string:

Required => É necessário digitar o {0}.

and finally decorate your view model property with this custom attribute:

public class MyViewModel
{
    [MyRequired]
    public int Foo { get; set; }
}

3 Comments

So for the same concept the ASP.NET MVC team is using two different constructs?
@mare, we can only hope that this will be fixed/made easier in future versions of MVC.
For this to work on client-side in ASP.NET MVC 3 you will need to either register the custom attribute (stackoverflow.com/a/5954452/169) or use AttributeAdapter approach (stackoverflow.com/a/5207912/169).
0

How about

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

public string Foo { get; set; }

Required => É necessário digitar o {0}.

Or

Required => {0} field is required.

Please refer below link for complete asp.net mvc localisation guide asp.net mvc localisation guide

2 Comments

how to make it global so it doesn't need to add (ErrorMessageResourceName="Required",ErrorMessageResourceType=typeof(Messages). or how to make error message for the field needed a integer value but user enters 'abc'
not sure what you mean by - make it global but for integer value error message - Does this help? stackoverflow.com/questions/9522369/…
0

Just add like this

[Required(ErrorMessage = "Your Error Message, here..!")]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.