Localization Support in Validator Tags

Topics: Validation Application Block
Apr 15, 2011 at 9:22 PM

I am trying to determine the best way of supporting Localization in Validator messages. It appears MessageTemplateResourceName andMessageTemplateResourceType should do the trick for Message Templates. However, I don't seem to find a similar construct in case of the Tag property of validator attribute decorators. If I embed a string in the Tag property of the attribute, in another culture, the final validation failure message will be a mix of two languages: English for the Tag part and another language for the rest of the message.

Is there some way to declaratively or programmatically set the Tag property value from an external resource file?

A related question: is it possible to assign an overall MessageTemplate string at the method level if I am doing validation at the method argument level. So, for instance, lets say I have the following code:


public void DoSomething
                    string a,
                    string b

I want to display a message like "DoSomething Failed" and pre-pend to whatever message the validators returned. So I want to always have the validators display a generic message like "'A' cannot be null'" but the method level message adds context to the error message so user knows where exactly the validation failed. i.e. the final user message would be 'Do Something Failed: 'A' cannot be null"

Obviously, my preference is to be able to pull this method level message from a resx file so I can localize the code later to other cultures.

Apr 18, 2011 at 2:11 AM

Sorry but the behaviors you want aren't supported out-of-the-box.  Of course modifying the source code is always a possible solution.  On the matter of localization feature for the Tag property, there are several approaches which I could think of but admittedly, they involve a lot of work.  (I'm merely presenting possible options, not necessarily suggesting all of them :)) 

1. Create a wrapper class for the ValidationResult so that the wrapper class has localization feature for the Tag property.

2. Create custom validators wrapping the existing validators

3. Create custom attributes which allows you to set the Tag property to the key in the resource file, pretty much similar to how you use the MessageTemplateResource and MessageTemplateResourceType properties.

Obviously, the easiest would be the first one but I'm not sure if it will violate your design or any of your specific requirement.

On your second concern, I assume you're using the ValidationCallhandler.  The exception message generated by this handler is "Parameter validation failed." and unfortunately, this isn't configurable as well.  You would have to create your custom call handler to accomplish what you want.


Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.