[asp.net mvc] Error Message - chung mà riêng

Are you using Entity Framework with ASP.NET MVC, along with client-validation?

For each ```DataAnnotation`` attribute you have a different error message, and you stuff them all into a Resource?

There are a few ways to work around some of those drawbacks ;)

1. Generic Customized Error Message

Generic means, well, generic. Use one message for many properties of the same kind, changing only the parts that need changing

For example:

The name must be shorter than 15 characters 
The address must be shorter than 150 characters

In the example above, name, address, 15 and 150 are the parameters we have to change in the message. So how do we do it?

Digging into the source code of each kind of DataAnnotation, we find a few interesting things

Source code for DataAnnotation https://github.com/Microsoft/referencesource/tree/master/System.ComponentModel.DataAnnotations/DataAnnotations

Every Attribute overrides the FormatErrorMessage method. Digging into each attribute’s implementation tells us the order of the error message’s parameters.

Attribute Source code Explanation
StringLength String.Format(CultureInfo.CurrentCulture,
errorMessage,
name,
this.MaximumLength,
this.MinimumLength);
return;
{0} = name,
{1} = MaximumLength,
{2} = MinimumLength
Compare String.Format(CultureInfo.CurrentCulture,
ErrorMessageString,
name,
OtherPropertyDisplayName ?? OtherProperty);
return;
{0} = name,
{1} = OtherPropertyDisplayName / OtherProperty
MinLength string.Format(CultureInfo.CurrentCulture,
ErrorMessageString,
name,
Length);
return;
{0} = name,
{1} = Length

So instead of writing out each error message directly, we just use string.Format with the exact order of the parameters

For example, with StringLength

[StringLength(70, ErrorMessage = "{0} phải chứa ít hơn {1} ký tự")]
public string Name { get; set; }

2. Error Message Localization

Actually the trick below not only helps you localize the Error Message strings, but any string you want

You have to create a Resource file in the project (.resx) to use it

More information here: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization

2.1. If you have Resharper

Resharper is a product from JetBrains that helps devs quite a bit

Put the cursor on any string -> Resharper -> Refactor -> Move…

resharper move to resource

Resharper charges for a license

2.2. If you want the free option

Visual Studio has a very nice extension, RestXManager. It does much the same as Resharper, and even a bit better

  • Open source
  • Active Development
  • Supports translating text

You can find and download it directly in Visual Studio: Tools > Extensions and Updates > Online > search "ResXManager"

ResXManager

or

at this link: https://marketplace.visualstudio.com/items?itemName=TomEnglert.ResXManager

Then, same as with Resharper, put the cursor on any string > right click > Move to Resource

Move to Resource

There are a few more tricks with the other kinds of DataAnnotation too