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.

mare
  • 13,033
  • 24
  • 102
  • 191
daniel
  • 1,929
  • 4
  • 14
  • 13

4 Answers4

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

takepara
  • 10,413
  • 3
  • 34
  • 31
9

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; }
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • So for the same concept the ASP.NET MVC team is using two different constructs? – mare Mar 07 '11 at 13:02
  • @mare, we can only hope that this will be fixed/made easier in future versions of MVC. – Darin Dimitrov Mar 07 '11 at 13:36
  • 1
    For this to work on client-side in ASP.NET MVC 3 you will need to either register the custom attribute (http://stackoverflow.com/a/5954452/169) or use AttributeAdapter approach (http://stackoverflow.com/a/5207912/169). – Arnold Zokas Jan 16 '12 at 01:26
0

Just add like this

[Required(ErrorMessage = "Your Error Message, here..!")]
Karthikeyan P
  • 1,216
  • 1
  • 20
  • 23
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

Community
  • 1
  • 1
swapneel
  • 3,061
  • 1
  • 25
  • 32
  • 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' – mahdi gh Aug 25 '13 at 07:11
  • not sure what you mean by - make it global but for integer value error message - Does this help? http://stackoverflow.com/questions/9522369/how-use-data-annotations-for-verifying-input-particular-type – swapneel Aug 25 '13 at 10:34