5

I have an old C# MVC 2.0 web application.

Whenever I use a [Required] attribute, the default validation error message goes:

The [whatever] field is required.

My problem is that the application isn't in English, so I basically have to change the attribute call to [Required(ErrorMessage = "Le champ [whatever] est requis.")] everywhere.

Is there a way to override the default error message so I only have to specify it when I want a specific message?

I'm looking for something like:

DefaultRequiredMessage = "Le champ {0} est requis.";
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
Rafalon
  • 4,450
  • 2
  • 16
  • 30
  • I didn't get, what's your problem with `[Required(ErrorMessage = "MyNotEnglishMessage")]`? – Salah Akbari Oct 29 '18 at 09:14
  • I don't want to have to write the same message everywhere in my app, I'd like to follow the DRY principle – Rafalon Oct 29 '18 at 09:15
  • 1
    Could you not just install the French language pack and set CurrentUICulture to a French culture (e.g. fr or fr-FR)? Or if you don't want to install a language pack, use a custom resource manager: https://stackoverflow.com/questions/2347650/how-to-use-dataannotations-errormessageresourcename-with-custom-resource-solutio – Joe Oct 29 '18 at 09:43
  • 1
    @Joe this is a very old app, so I don't even know if I could find the French language pack, and I don't really want to take a risk to mess up everything if it doesn't exactly match the one needed. I think S.Akbari's solution is close to your second option, so I'd rather see if this works as expected as it has little chances to mess up things. However, I agree that having a French language pack would be a great solution! – Rafalon Oct 29 '18 at 09:57

2 Answers2

8

You can create a class and inherit it from RequiredAttribute. Something like this:

public class CustomRequired: RequiredAttribute
{
    public CustomRequired()
    {
        this.ErrorMessage = "Le champ est requis.";
    }
}

Or:

public class CustomRequired: RequiredAttribute
{
    public override string FormatErrorMessage(string whatever)
    {
        return !String.IsNullOrEmpty(ErrorMessage)
            ? ErrorMessage
            : $"Le champ {whatever} est requis.";
    }
}

You should use CustomRequired with your properties, rather than [Required].

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
  • How would you change it to include the field name in the error message? – Rafalon Oct 29 '18 at 09:23
  • 1
    Shouldn't it be `CustomRequiredAttribute` instead of `CustomRequired`? Anyway thank you for this, let me just try it and if it works as expected I'll accept this answer – Rafalon Oct 29 '18 at 09:31
  • @Rafalon It is just the name of your custom class. Feel free to name it to whatever you like :) – Salah Akbari Oct 29 '18 at 09:32
  • 1
    Yeah, I just thought it should end with `Attribute` so we can use `[CustomRequired]` – Rafalon Oct 29 '18 at 09:33
1

Please perform the below operation to override default required error message:

  • Create a custom adapter inheriting the RequiredAttributeAdapter as below :

public class YourRequiredAttributeAdapter : DataAnnotationsModelValidator<RequiredAttribute>
{
    public YourRequiredAttributeAdapter(ModelMetadata metadata, ControllerContext context, RequiredAttribute attribute)
        : base(metadata, context, attribute)
    {
        if (string.IsNullOrWhiteSpace(attribute.ErrorMessage)
            )
        {
            attribute.ErrorMessageResourceType ="Your resource file";
            attribute.ErrorMessageResourceName = "Keep the key name in resource file as "PropertyValueRequired";
        }
    }

}
  • Register the Adapter in Global.asax Application_Start() as below:

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

  • Resource File Configuration: enter image description here
VCody
  • 506
  • 3
  • 12