Compartir a través de


Entering Configuration Information

These procedures explain how to configure the Validation Application Block with the configuration tools. The Validation Application Block also allows you to use attributes and code to perform many of the tasks described here. For information about how to use attributes and code, see Using the Validation Block Validators. For information about properties that are associated with validators such as Tag and Message Template, see Understanding Common Validator Properties.

This procedure explains how to add the Validation Application Block to the configuration file. For details of the schema for the Validation Application Block configuration, see Source Schema for the Validation Application Block.

To add the Validation Application Block by using the configuration tool

  1. Open the configuration file. For more information, see Configuring Enterprise Library.
  2. Open the Blocks menu and then click Add Validation Settings.
  3. (Optional) If you want to encrypt the configuration file, click the chevron expander arrow to show the properties, and then select a protection provider from the drop-down list in the Protection Provider field.

The next procedure shows how to define a rule set for the members of a type. It assumes that you have already added the Validation Application Block to your configuration. Members of a type that you will validate can be fields, methods, or properties. Note that it is possible for a type to have multiple rule sets associated with each type you configure. For example, in the following screenshot, the Product type has two rule sets, Ruleset1 and Ruleset2.

  • Ruleset1 specifies a not null validator and a regular expression validator for the ID property, and a string length validator for the Name property.
  • Ruleset2 specifies a not null validator and a string length validator for the ID property, and a range validator for the InStock property.

Ff664693.28652470-bcc8-4f8e-bacd-269ec694750a(en-us,PandP.50).png

When you click on an item in the configuration tool, related items are highlighted and links appear between them, as shown for the Property: ID item in the screenshot.

To define a rule set for members of a type by using the configuration tool

  1. If the settings for the Validation Settings section are not visible, click the child expander arrow to the left of Validation Settings.
  2. Click the plus sign icon in Validated Types and click Add Type to Validate.
  3. In the type selector dialog box, expand the assembly you want to use. To filter the list, type part of the name in the Type name edit box; for example, type "string" to filter for all classes containing the word "string". If the assembly is not shown in the list, click Add from GAC (the global assembly cache) or Add from File and navigate to it. Select the type you want to validate and click OK.
  4. To define a rule set, right-click on the type in the Validated Types column and then click Add Validation Ruleset. This adds a rule set with the default name Validation Ruleset and a Validators item that you can use to validate the type itself.
  5. Edit the Name property of the rule set as required.
  6. To add a validator that applies to an instance of a class as a whole, rather than to individual members of that class, right-click the Validators item in the Validation Targets column, point to Add Validators, and then click the validator you want to apply. Repeat this step to add additional validators that will be applied at the type level if required. Typically you will use this feature for only a not null validator, object collection validator, composite validator, or a custom validator.
  7. To select the individual members of a type to be validated, you can use either of the following methods:
    • Right-click on the heading of the rule set item, and then click Add Field to Validate, Add Method to Validate, or Add Property to Validate. Then enter the name of the field, method, or property in the Validation Targets pane.
    • Alternatively, you can select several members of a type simultaneously. Right-click on the rule set and click Select Members. In the Member Selector dialog box, select the Properties, Methods, and/or Fields that you want to validate, and then click OK.
  8. To add a validator for a type member, right-click the heading of a field, method, or property of a member for the type in the Validation Targets column, point to Add Validators, and then click the validator you want to apply. Repeat this step to add additional validators that will be applied to individual type members as required.
  9. Edit the properties of each validator you added to the configuration:
    • (Optional) Edit the default Name property.
    • Specify the validation error message. Enter either a Message Template (which may include the validation message tokens described in Understanding Common Validator Properties), or set the Template Resource Name and Template Resource Type properties if you want to load the message template from a resources file. To use a resources file, enter the name of the resource for the Template Resource Name then click on the ellipsis button (...) in the Template Resource Type property and use the type selector to locate and select the resources file.
    • (Optional) If you want the validator to operate in reverse, so that the validator will return false (failed validation) when the validation rule is satisfied, and true (no error) when the validation test fails, set the Negated property to True. The default is False.
    • (Optional) If you want to pass an additional text value to the application when validation fails, enter this text as the Tag property. You can filter validation results on the values you specify for this property.
    • Enter values for the remaining validator properties. The properties available differ for each type of validator. For a list of properties for each type of validator, see Using the Validation Block Validators.

The next procedure explains how to define an AndCompositeValidator or an OrCompositeValidator. Composite validators contain individual validators that are combined with a Boolean AND or OR operation. For example, the following screenshot shows an Or composite validator applied to the ID property of a type in Ruleset1. The validation specifies that either the rule defined by a property comparison validator or the rule defined by a range validator must be satisfied for validation of the ID property value to succeed.

Ff664693.6a8d93b0-d1ef-4e48-91a4-0d28087aaaa9(en-us,PandP.50).png

You can also nest composite validators to create complex logic for a member, such as (A OR (B AND C)).

To define composite validators

  1. Right-click the member of the type you want to validate, or on the Validator item to add a validator for the type itself, and click Add Validators. Then click either Add And Composite Validator or Add Or Composite Validator.
  2. Right-click the And Composite Validator or the Or Composite Validator you added, point to Add Validators and then click one of the validators that will be a part of the composite validator. Repeat this step to add additional validators as required.
  3. Edit the properties of the And Composite Validator or the Or Composite Validator, and edit the properties of each validator you add to the composite validator:
    • (Optional) Edit the default Name property.
    • Specify the validation error message. Enter either a Message Template (which may include the validation message tokens described in Understanding Common Validator Properties), or set the Template Resource Name and Template Resource Type properties if you want to load the message template from a resources file. To use a resources file, enter the name of the resource for the Template Resource Name then click on the ellipsis button (...) in the Template Resource Type property and use the type selector to locate and select the resources file.
    • (Optional) If you want the validator to operate in reverse, so that the validator will return false (failed validation) when the validation rule is satisfied, and true (no error) when the validation test fails, set the Negated property to True. The default is False.
    • (Optional) If you want to pass an additional text value to the application when validation fails, enter this text as the Tag property.
    • Enter values for the remaining validator properties. The properties available differ for each type of validator. For a list of properties for each type of validator, see Using the Validation Block Validators.