Localizing Validation Using DataAnnotations

Problem: I’m working this project using ASP.NET MVC, and I’d like to use the MVC’s built in validation at the same time localize it to produce culture specific error messages.  On of the issues I was having is trying to get the DataAnnotation’s to use my existing global resources that were in the sites “App_GlobalResources”.  This didn’t work because DataAnnotations use strong typed resource class names so they can be used in other applications like Silverlight, WinForms, etc.


The solution is pretty simple but requires a little extra work on your part.  Once it’s setup, adding culture specific messages is a breeze.  In this example, my annotated classes live in a separate assembly from the main application.

1.  First off we need to add resources to the assembly that has our classes that will be annotating.  I create a folder called “Resources” for organization, as shown below.

resources_folderThere are a couple of ways you can add you resource files to you project.  You can either manually add them by right clicking on the folder and adding the new item, or you can try my DotNetLocalizer tool which will automate most of it for you.  Please note that the tool is a work in progress, but it does utilize Google translator which makes auto translation of your resources very fast.  When you add you resources there are a few important points to note.  First, you need to open the resx designer (double click the resx file) and set the Access Modifier to Public.  This is important so the DataAnnotations can access the resource data.


Now verify and set the following properties on all the resx files.  Right click the resx file and select Properties.  The Build Action is usually set to this by default but it never hurts to double check.

resx_propsNow based on the above setup, you should have strongly typed resources in your assembly.  Using the example assembly pictured above, the resource class would be Wictbm.Core.Resources.Validation.

2.  Now comes the fun part with the annotations.  Below shows a snippet of a class I have with annotated properties.  The annotations are the three items that mark the property “HousingCost”: Required, Range, and DisplayFormat.  Of the three, the one to notice is the Required attribute’s named parameters ErrorMessageResourceName and ErrorMessageResourceType.  The ErrorMessageResourceName is the resource key value, and the ErrorMessageResourceType is the class key value which it strongly typed.  As you can see the value for ErrorMessageResourceType is the class “Resources.Validation”, which is short for the full path of Wictbm.Core.Resources.Validation.

dataannotation 3.  Lastly, we need to do some validation.  Since I was working on a ASP.NET MVC application, I will demonstrate it below (note that I am using version 2 of mvc).  Other applications follow the same type of concepts.  Highlighted in the image below shows the html helper used to trigger the validation messages should there be any validations that fail.  In Step 2’s image shows this property “HousingCost” as Required.


Let’s see what happens when our UI culture is set to Spanish and the form is submitted with this property blank.  First I’ll show the before then the failed validation.  As we can see, the message before form submission shows no error, then after submitting the form with no data present in the text box results in the error message that I’ve highlighted in yellow.


That’s it.  You can also add client validation which makes use of the DataAnnotation’s in the web site.  A good example of this is shown in Scott Gu’s Model Validation post.  If your working on a ASP.NET site and are curious how I set the user’s culture on the page request see my blog on Setting Culture For ASP.NET Page Request.

No comments: