Validation Architecture ” IValidator , BaseValidator , and CustomValidatorValidation 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:
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 . |