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 QuickStart configuration file. They assume that you already have an application file open, and that you have included the Validation Application Block. Additionally, the validation rule sets created in the previous walkthrough should be in place. For general information on 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 select View Code.

  3. Search for the following code block:

    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);
       }
    }
    
    'Usage
    Private Sub validateCustomerButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles validateCustomerButton.Click
        Dim customer As Customer = CreateCustomer()
        If Not customer Is Nothing Then
            Dim validator As Validator(Of Customer) = ValidationFactory.CreateValidator(Of Customer)(customerRuleSetCombo.Text)
            Dim results As ValidationResults = validator.Validate(customer)
            DisplayValidationResults(results, customerResultsTreeView)
        End If
    End Sub
    

    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.

  4. Comment out the following lines:

    Validator<Customer> validator = ValidationFactory.CreateValidator<Customer>(customerRuleSetCombo.Text);
    ValidationResults results = validator.Validate(customer);
    
    'Usage
    Dim validator As Validator(Of Customer) = ValidationFactory.CreateValidator(Of Customer)(customerRuleSetCombo.Text)
    Dim results As ValidationResults = validator.Validate(customer)
    DisplayValidationResults(results, customerResultsTreeView)
    
  5. Add the following line of code immediately after the commented lines.

    ValidationResults results = Validation.Validate<Customer>(customer, customerRuleSetCombo.Text);
    
    'Usage
    Dim results As ValidationResults = Validation.Validate(customer, customerRuleSetCombo.Text)
    

    The application now uses the Validation façade to validate customer information.

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.