Geeks With Blogs
Daily dotnet {notes on codes}

The documentation installed with Enterprise Library 3.0 also contains comprehensive details and examples of how you can use attributes to apply validation rules, how to create custom validators, and how to use self-validation. Once you are familiar with the basic process for creating and using rule sets and the validators from the Validation Application Block, you will find it easy to extend the techniques to implement all kinds of custom validation mechanisms.

The Validation Application Block Built-in Validators

The Validation Application Block contains a wide range of validators that you can use to check almost any type of property or value. The built-in validators include:


  • Range validators, such as the generic Range Validator, the Date Time Range Validator, and the String Length Validator. These validators have properties that define the upper and lower bounds, and the bound type (Inclusive, Exclusive, or Ignore) that allow you to specify exactly what value range is valid. If you set a bound type to Ignore, the validator does not check that boundary, so you can use them to specify that valid values are greater than or less than a specific value, as well as between two values.
  • Comparison validators, such as the Property Comparison Validator that checks the value against a specified property of a specified object, and the Relative Date Time Validator that checks if the difference between the value and a specified DateTime is within a valid range.
  • The Not Null Validator, which requires a value to be present.
  • Validators that perform specific tests on string values, such as the Regular Expression Validator, the Contains Characters Validator, and the Type Conversion Validator that checks if a string value can be converted to a specified value type.
  • Validators that check if a value is in a specified set, such as the Domain Validator that checks against a list of values you provide, or the Enum Validator that checks against a specified enumeration.
  • Validators that operate on custom objects, such as the Object Validator that activates validation of an object instance using rules applied to the object, and the Object Collection Validator that does the same for a collection of object instances.
  • Composite validators, such as the And Composite Validator and the Or Composite Validator. These validators allow you to combine other validators by nesting them within this type of validator to create complex validation rules.

The validators have specific properties, depending on the validation process they carry out. However, all of the validators also have some common properties:


  • Tag, which can contain text you use to categorize or filter results, or insert into the validation error message. You can access the Tag value in code when validating rule sets.
  • Negated, which reverses the operation of the validator so that it returns False if the value is valid, and True if not. This is useful when you combine validators using the And Composite Validator and the Or Composite Validator.
  • MessageTemplate, where you specify the validation error message. You can include the placeholders {0}, {1}, and {2} to insert the validated value, the target object member name (if available), and the value of the Tag property of the validator.
  • MessageTemplateResourceName and MessageTemplateResourceType. You can use these properties to retrieve a validation error message string from the application resources assembly. If you create resources for multiple cultures, the validator will automatically retrieve the string for the current culture.

Posted on Sunday, February 14, 2010 7:48 PM | Back to top

Comments on this post: The Validation Application Block Built-in Validators

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © Prasenjit | Powered by: