Walkthrough: Validate an Object

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 walkthrough explains how to validate an object. This can be done either by using the Validation façade or by directly calling an instance of the Validation object. These procedures are specific to the QuickStarts. They assume that the validation rule sets created in Walkthrough: Use Configuration to Define a Validation Rule Set are in place. For general information about how to configure the Validation Application Block, see Entering Configuration Information.

To validate an object

  1. In Visual Studio, open the ValidationQuickStart project.
  2. Right-click MainForm.cs, and then click View Code.
  3. Find the following code block.
  4. private void validateCustomerButton_Click(object sender, EventArgs e)
    {
      Customer customer = CreateCustomer();
      if (customer != null)
      {
        Validator<Customer> validator = ValidationFactory.CreateValidator<Customer>(customerRuleSetCombo.Text);
        ValidationResults results = validator.Validate(customer);
        DisplayValidationResults(results, customerResultsTreeView);
      }
    }
    
    

    This event handler creates an instance of the Customer validator and uses it to validate against the currently selected rule set. Use this approach if you want to reuse the same validator instance to validate several different objects.

  5. Comment out the following lines.
  6. Validator<Customer> validator = ValidationFactory.CreateValidator<Customer>(customerRuleSetCombo.Text);
    ValidationResults results = validator.Validate(customer);
    
    
  7. Add the following line of code immediately after the commented lines.
  8. ValidationResults results = Validation.Validate<Customer>(customer, customerRuleSetCombo.Text);
    
    
  9. The application now uses the Validation façade to validate customer information.


Show: