Validation Architecture IValidator , BaseValidator , and


Validation Architecture ” IValidator , BaseValidator , and CustomValidator

Validation in ASP.NET involves two categories of controls: validators and validation targets . Validators are the controls that perform validation, and validation targets are the controls that expose properties that need to be validated . The page developer makes the association between a validator and a validation target.

Validation targets are usually input controls ”in other words, controls that render as form elements that allow data entry. Any control that contains a ValidationPropertyAttribute in its metadata can be a validation target. In general, these controls provide access to the user 's entry via a single property. To support validation, the control identifies this property for validation by using the ValidationPropertyAttribute metadata attribute. For example, the Text property of a TextBox is marked as the property that needs to be validated using this attribute. We will look at an example of a validation target in Chapter 21, "DHTML-Based Server Controls."

A validator control implements a specific validation rule. Each validator control instance can be associated with a single validation target instance. Validators also have an associated error message that is rendered when the user data is invalid. The page developer can associate multiple validators with a single validation target. This allows multiple validation checks to be performed on a single value. Having multiple validators bound to the same validation target also allows page developers to use a different error message for each validation rule that fails.

By definition, any class that implements the System.Web.UI.IValidator interface qualifies as a validator in the ASP.NET validation framework. The IValidator interface is defined as follows :

 publicinterfaceIValidator{ stringErrorMessage{get;set;} boolIsValid{get;set;} voidValidate(); } 

The intent behind defining this interface was that component developers could implement custom validators by implementing IValidator . However, in version 1.0 of ASP.NET, this is not possible. The implementation of the validation framework is distributed across the Page class; the BaseValidator class (which is the base class for all the built-in validators); and controls that trigger validation, such as Button and LinkButton . The interdependencies of these components do not allow arbitrary IValidator implementations . However, it is still possible to create custom validation controls by deriving from BaseValidator instead.

BaseValidator is an abstract base class that derives from the Label class. BaseValidator implements the IValidator interface. In addition, this base class performs several tasks that are common to all validators:

  • Exposes the ControlToValidate property, which allows page developers to associate validators with validation targets. Exposes the GetControlValidationValue method to extract the value from the associated control that needs to be validated.

  • Defines the EvaluateIsValid abstract method that you must override in your validator to implement your validation logic.

  • Registers itself with the Validators collection of the Page in its OnInit method so that a validator can participate in the validation framework.

  • Generates script to implement the client-side validation functionality if the client supports scripting. Exposes the RenderUplevel property, which is set to true when it determines that the browser making the request contains the features needed to implement client-side validation.

  • Automatically renders the validator when its IsValid property is false ” in other words, when the associated validation target contains an invalid entry.

In short, BaseValidator provides the plumbing needed to participate in the validation feature of Web Forms and simplifies the implementation of custom validators.

ASP.NET also provides a built-in validator named CustomValidator . The name of this validator suggests that it could be a base class for custom validators. However, CustomValidator is actually a validator control that page developers can use to add custom validation logic. This control exposes a ServerValidate server-side event and a ClientValidationFunction property to allow the page developer to supply server-side and client-side validation logic. CustomValidator does not provide a reuse mechanism for control developers. Furthermore, it does not get invoked to perform its validation when the value to be validated is empty. Therefore, we recommend that you derive new validators from BaseValidator instead of extending CustomValidator . The StringValidator example that we will examine later in this chapter is consistent with this recommendation and derives from BaseValidator .



Developing Microsoft ASP. NET Server Controls and Components
Developing Microsoft ASP.NET Server Controls and Components (Pro-Developer)
ISBN: 0735615829
EAN: 2147483647
Year: 2005
Pages: 183

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net