MessageTemplateResourceType

Topics: Validation Application Block
Feb 19, 2007 at 11:06 PM
I am decorating my class with ValidatorAttributes. I want to use MessageTemplateResourceName and MessageTemplateResourceType but I am having difficulty with the latter?

Can someone please post an example of how to do this?

StringLengthValidator(1, 5, Ruleset = "RuleSetA", Tag = "Description Text Length to long", MessageTemplateResourceName= "DescriptionStringLengthTooLong", MessageTemplateResourceType = ???)
public string Description
Feb 20, 2007 at 1:10 AM
Edited Feb 20, 2007 at 1:11 AM
You want something like
StringLengthValidator(..., MessageTemplateResourceType=typeof(Resources))

...where Resources is the name of the class generated from your .resx file.

BTW from your example I suspect you're misinterpreting the use of the Tag property. The intent is for this to be a way of categorizing validation errors, such as by marking them as warnings, errors, critical etc. The feature is most useful if you're likely to generate multiple validation results that share the same tag.

Hope this helps
Tom
Mar 23, 2007 at 7:33 PM
How to make generated resource class public from file in "App_GlobalResources" folder ?
Mar 27, 2007 at 6:56 AM
You don't actually need to make it public, the Validation-Block uses reflection to access your resource type so it doesn't really matter what modifier you use, internal works just fine.

The ResourceType should be the assembly qualified name (see the msdn help for this), like this messageTemplateResourceType="Test.ErrorMessages, Test, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" (where Test is the Assemlby and Namespace name and ErrorMessages the name of the generated resource class). If you just put "Test.ErrorMessages" in there, it won't work.

Tom: Could you explain the tag property a little more detailed? Is this a feature of the Errorprovider and how can I assign different icons (orange, green, red) to the validated properties?

Mar 27, 2007 at 4:16 PM
testalino: The Tag property lets you assign an arbitrary string to each individual validation operation (for example, each validator assigned to a property of a class may have a different Tag). If any validator fails, the ValidationResult object that is generated will be marked with the same tag from the failing Validator. You can also take the complete ValidationResults collection and use the FindAll method to filter the list by tag.

The most obvious scenario for this feature is assigning severities to different validators. For example you may assign a number of validators to a class and assign some of them a tag of "Critical" and some of "Warning". After validating the object, you can use ValidationResults.FindAll to check the number of critical failures versus warnings.

You could certainly implement something that used the tags to display different icons to the UI, but we don't provide any built-in support for this.

HTH
Tom