Enterprise Library Validation MessageTemplate Tokens

1 minute read,

I always forget… pulled from MSDN with my notes added in italics:

Understanding Message Template Tokens

Custom message templates can contain tokens. The validator replaces these tokens with values before it adds the resulting message to an instance of the ValidationResult class. Tokens are represented by using the strings {0}, {1}, {2}, and so on within the message template strings. All validators provided by the Validation Application Block use the first three tokens {0}, {1}, and {2} for the same purposes. Different validators may also understand additional tokens, beginning with {3}. The following table describes tokens {0}, {1}, and {2}.

token description
{0} This token represents the value of the object that is being validated. Although it can be useful to show the original value as a part of the validation message, you must be careful to avoid injection attacks by escaping any characters that can be used to attack the system that conveys the message to the user.
In simple terms: returns “ObjectBeingValidatedFullTypeName+Field”
{1} This token represents the key of the object that is being validated. When the validator is attached to a member of a type such as a property or a field, the key is set to the member name. When the validator is attached to an object, the key is null and the token is replaced by an empty string.
In simple terms: returns “Field” name being validated
{2} This token represents the tag that is specified on the validator instance. If no tag is supplied, the token is replaced by an empty string.
In simple terms: returns contents of [Validator Tag=”foo”] property defined in the attribute.

Leave a comment

Your email address will not be published. Required fields are marked *

Loading...