Implementing a Custom Validator

The Validation Application Block provides extension points to implement custom validators; both loosely and strongly typed validators can be implemented using the abstract classes Validator and Validator<T> respectively. We may also inherit from an existing Validator class to extend the functionality. Additionally, we can also implement a custom Validator Attribute to allow our custom validator to be used with the attribute-based validation approach.

Let us implement a simple US Zip Code validator to understand the implementation details of a custom Validator. The steps to implement it are as follows:

  1. The very first step is to add the required assembly references. We need the given assemblies for the implementation:
    • System.Configuration.dll
    • Microsoft.Practices.EnterpriseLibrary.Common.dll
    • Microsoft.Practices.EnterpriseLibrary.Validation.dll
  2. Add a class and name the class USZipCodeValidator; this class will be decorated with the ConfigurationElementType attribute and we will use the CustomValidatorData as the configuration object. CustomValidatorData describes an instance of a custom Validator class.
    [ConfigurationElementType(typeof(CustomValidatorData))]
    public class USZipCodeValidator
    {
    }
    
  3. Next, we can inherit using the strongly typed Validator and implement the abstract members, additionally the default message template, and the required constructors. We also have to provide our US Zip Code validation logic in the DoValidate method.
    [ConfigurationElementType(typeof(CustomValidatorData))]
    public class USZipCodeValidator: Validator<string>
    {
    public USZipCodeValidator() : base(null, null) { }
    public USZipCodeValidator(string messageTemplate, string tag) : base(messageTemplate, tag) { }
    protected override void DoValidate(string objectToValidate, object currentTarget, string key, ValidationResults validationResults)
    {
    string zipCodePattern = @"d{5}(-d{4})?";
    Regex regex = new Regex(zipCodePattern);
    if (!regex.IsMatch(objectToValidate))
    {
    string message = string.Format(this.MessageTemplate, objectToValidate);
    this.LogValidationResult(validationResults, message, currentTarget, key);
    }
    }
    protected override string DefaultMessageTemplate
    {
    get { return "Value {0} is not a valid US Zip Code"; }
    }
    }
    

We have implemented our custom validator that validates a string for a valid US Zip Code. The USZipCodeValidator class can now be consumed either through configuration by adding the custom validator or through programmatic validation. Let us now see how we consume the Validator in our application through programmatic validation to validate the user input.

The following code snippet demonstrates the usage of the implemented custom validator, which can also be leveraged using the configuration-based approach by adding the USZipCodeValidator using the Add Custom Validator menu item while adding validators.

USZipCodeValidator customValidator = new USZipCodeValidator();
ValidationResults results = customValidator.Validate(textBoxUSZipCode.Text);
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.105.215