The Java Implementation
A high-level class diagram of the validation engine is shown in Figure 1:
|Figure 1. Validating User Input: This high-level class diagram shows the relationships between the classes that will perform the validation for each user input field.|
class maintains the repository of all form definitions loaded for an application.
class maintains the definition of a form. A form definition consists of a set of individual field definitions. Each field definition specifies validation and other details about the field.
class provides the abstract definition of a field in a form. A field definition describes the validation and parsing criteria for a form field, as well as additional display attributes.
Each of the subclasses of FieldDefinition
implements concrete behaviors for the type of field it implements.
class represents an instance of a form for which input is being received or whose data is being presented. A form has one or more Field
instances that contain the actual field data. Each form is backed by a FormDefinition
and each field in a form is backed by a FieldDefinition
. All parsing and validation tasks are delegated to the definitions, therefore making a form and its fields a non-type specific container for form data (see Figure 2
Extending The Data Validation Engine
An application developer can also add more field types or more complex validation logic as needed with the above architecture. For example, the user input to a ZIP code text field might need to be validated against a regular expression that formally defines the validity rules of ZIP codes. In this case, the developer may subclass the CustomField
class to provide her own ZipCodeDefinition
class that handles ZIP code field definitions such as:
<custom-field key="postal-code" required="true">
|Figure 2. Forming a Sequence: This sequence diagram shows activity in the validation engine at request processing time.|
The behaviors of existing field definition types described in the Java implementation section above can also be customized by extending the actual definition implementation class and specifying an extra "class" attribute in the field definition element in the XML file. For example, one application might need to verify that an input to a decimal type "Amount To Transfer" field has to be less than the existing funds in the account of the current customer. Instead of directly hard-coding this piece of validation logic in the servlet code, a developer may extend the existing DecimalFieldDefinition
class in a new TransferAmountDefinition
class, which involves reading the customer account balance from a database and validating the transfer amount value against that data.