Walkthrough: Use Configuration to Define a Validation Rule Set

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.

This section explains how to use configuration to associate a rule set with a property named PostCode and how to create a rule set named "RuleSetB". These procedures are specific to the QuickStart configuration file. They assume that you already have an application file open and that you have included the Validation Application Block. For general information about how to configure the Validation Application Block, see Entering Configuration Information.

To associate a validator with the PostCode property

  1. Open a configuration file in the configuration editor. If you are working on the ValidationQuickStart application, open Designtime.App.config. If you are working on the ValidationAspNetQuickStart application, open Web.config.
  2. Right-click the Validation Application Block node, point to New, and then click Type. The Type Selector — System.Object dialog box opens.
  3. Click the Load an Assembly button.
  4. Navigate to the <QuickStarts install path>\Validation\CS\BusinessEntities\bin\Debug directory.
  5. Select ValidationQuickStart.BusinessEntities.dll, and then click Open.
  6. In the Type Selector — System.Object dialog box, expand the ValidationQuickStart.BusinessEntities node, and then double-click Address.
  7. Right-click the newly created Address node, point to New, and then click Rule Set.
  8. Select the newly created Rule Set node. In the right pane, click in the Name property, and then rename Rule Set to USAddress.
  9. Right-click USAddress, point to New, and then click Choose Members. The Member Selector dialog box appears.
  10. Under Properties, select PostCode, and then click OK.
  11. Right-click PostCode, point to New, and then click Regular Expression Validator.
  12. Select the newly created Regular Expression Validator node. In the right pane, set the properties:
    • Click in Pattern and click the ellipsis button (…). The Regular Expression Editor appears. Select U.S. Zip code, and then click OK.
    • Click in MessageTemplate and enter: Must be a valid U.S. zip code

The next procedure explains how to create a rule set named "RuleSetB" and how to attach validators to the first three properties. You can follow the same steps to reproduce the rest of configuration.

To create RuleSetB

  1. Open a configuration file in the configuration editor. If you are working on the ValidationQuickStart application, open Designtime.App.config. If you are working on the ValidationAspNetQuickStart application, open Web.config.
  2. Right-click the Validation Application Block node, point to New, and then click Type. The Type Selector — System.Object dialog box opens.
  3. Click the Load an Assembly button.
  4. Navigate to the <QuickStarts install path>\Validation\CS\BusinessEntities\bin\Debug directory.
  5. Select ValidationQuickStart.BusinessEntities.dll, and then click Open.
  6. In the Type Selector — System.Object dialog box, expand the ValidationQuickStart.BusinessEntities node, and then double-click Customer.
  7. Right-click the newly created Customer node, point to New, and then click Rule Set.
  8. Select the newly created Rule Set node. In the right pane, click in the Name property, and then rename Rule Set to RuleSetB.
  9. Right-click RuleSetB, point to New, and then click Choose Members. The Member Selector dialog box appears.
  10. Select Properties, and then click OK. The properties appear in the configuration tree.
  11. Right-click Address, point to New, and then click Object Validator.
  12. Select the newly created Object Validator node. In the right pane, click in the Name property, and then rename Object Validator to Object Validator — USAddress.
  13. Click in the TargetRuleset property, and then enter: USAddress.
  14. Right-click DateOfBirth, point to New, and then click Date Range Validator.
  15. Select the newly created Date Range Validator node. In the right pane, set the properties.
    • Click in LowerBound and set it to 1/1/69.
    • Click in LowerBoundType and set it to Inclusive.
    • Click in UpperBoundType and set it to Ignore.
    • Click in MessageTemplate and enter: Must be born after 1969.
  16. Right-click Email, point to New, and then click Regular Expression Validator.
  17. Select the newly created Regex Validator node. In the right pane, set the properties.
    • Click in Pattern and click the ellipsis button (…). The Regular Expression Editor appears. Select Internet e-mail address, and then click OK.
    • Click in MessageTemplate and enter: Invalid e-mail address.

Follow the same approach to configure the other properties. If you open the file that accompanies the QuickStart with one of the configuration tools you can see the configuration hierarchy and all the properties.


Show: