skip to the main content area of this page
Patterns and Practices

Validation Application Block Message Template Tokens and Resource Files


The Validation Application Block in Enterprise Library 3.0 has Message Template Tokens and support for Resource Files to ease the burden of creating custom error messages. The following class uses a custom string length validation error message, called InvalidStringLengthMessage, in a Resource File, called Resource1.resx:

public class Tag
{
    private int _tagId;
    public int TagId
    {
        get { return _tagId; }
        set { _tagId = value; }
    }

    private string _name;

    [StringLengthValidator(1, 200,
        MessageTemplateResourceName = "InvalidStringLengthMessage",
        MessageTemplateResourceType = typeof(Resource1))]
    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }

    private string _description;

    [StringLengthValidator(1, 100,
        MessageTemplateResourceName = "InvalidStringLengthMessage",
        MessageTemplateResourceType = typeof(Resource1))]
    public string Description
    {
        get { return _description; }
        set { _description = value; }
    }
}

 

The resource file, Resource1, looks as follows:

 

Validation Application Block Resource File and Message Template Tokens

 

The InvalidStringLengthMessage Value includes Message Template Tokens that are specific to the StringLengthValidator:

  • {0} - The value being validated.
  • {1} - The Key of the value being validated.
  • {2} - The Tag of the validator instance.
  • {3} - The lower bound configured for the validator instance.
  • {4} - The lower bound type (Inclusive, Exclusive, or Ignore) configured for the validator instance.
  • {5} - The upper bound configured for the validator instance.
  • {6} - The upper bound type (Inclusive, Exclusive or Ignore) configured for the validator instance.

 

The tokens provide the ability to provide a single format for all Invalid StringLengthValidator Error Messages as opposed to creating an error message for each property that has a StringLengthValidator.

Instead of the Name Property having the default error message of:

"The length of the value must fall within the range "1" (Inclusive) - "200" (Inclusive)."

The error message is now:

Invalid Name, 1 - 200 Characters.

The use of Message Template Tokens and Resource Files really improve the creation and maintenance of custom error messages.


Tags: ValidationApplicationBlock, MessageTemplateTokens


Topics



Popular Tags



Recent Links