Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Java and XML: Learn a Better Approach To Data Validation : Page 3

Forget client-side script, hard-coded server-side data validation rules, and database retrieval. There's a better way to validate user input data using a combination of Java and XML.


advertisement

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.
The FormDefinitionRepository class maintains the repository of all form definitions loaded for an application.

The FormDefinition 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.

The FieldDefinition 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.

The Form 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">
      <label>Zip Code</label>
      <custom-attribute name="expression">
         ^\d{5}(-\d{4})??$
      </custom-attribute>
   </custom-field>
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.


Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date