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 4

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

WEBINAR: On-demand Event

Unleash Your DevOps Strategy by Synchronizing Application and Database Changes REGISTER >

Sample Form Definition
A sample forms definition file is shown below (the DTD specification has been omitted):

   <form-definitions>
       <form key="user-info">
           <text-field key="first-name">
               <label>First Name</label>
               <max-length>40</max-length>
           </text-field>
           <text-field key="last-name" required="true">
               <label>Last Name</label>
               <max-length>40</max-length>
           </text-field>
           <integer-field key="age" required="true">
               <label>Age</label>
               <default>25</default>
               <min-inclusive>18</min-inclusive>
               <max-inclusive>60</max-inclusive>
           </integer-field>
           <date-field key="birthday">
               <label>Birthday</label>
           </date-field>
       </form>
   </form-definitions>
As you can see, the form has an attribute "key," as does each field. These values provide the unique identifier with which to reference forms and fields. Here are detailed explanations of the field properties specified:

  • label
The label is used by the engine when generating error messages and can be used to specify the actual label text when rendering the form to submit.

  • required
The required attribute specifies that some input must be supplied for the field to be considered valid.

  • default
The default attribute specifies the text that the field should default to.

  • max-length
Applies to text fields. In the example, it is used to specify the maximum length in characters for the First Name and Last Name fields.

  • min-inclusive
Applies to integer or decimal fields. Specifies the minimum acceptable value for the Age field in the example.

  • max-inclusive
Applies to integer or decimal fields. Specifies the maximum acceptable value for the Age field in the example.

Inter-Field Validation Rules
It is common for applications to need to apply validation rules to a certain field depending on the result of validation on another field. For example, an application might validate a ZIP code field only if a user specifies that she is inputting a U.S. address. The DTD for the meta-language we described above can be augmented to allow inter-field rules such as:

   <form-definitions>
      <form key="addr-info">
         <text-field key="country" required="true">
            <label>Country</label>
            <max-length>40</max-length>
         </text-field>
         <text-field key="zip-code" 
            required="zip-code-rule">
            <label>ZIP Code</label>
            <max-length>10</max-length>
            <rule name="zip-code-rule" 
               type="Boolean">
               <eval opd1="country.input" 
                   opr="equals" opd2="United States"/>
            </rule>
         </text-field>
      </form>
   </form-definitions>
In the example above, the "required" attribute of the "zip-code" field is obtained at validation time by evaluating rule "zip-code-rule," which checks whether the input value of the "country" field equals "United States."

The Balanced Approach
Data validation must be flexible, scalable, and portable; typical solutions are deficient in at least one of these key areas. The Java/XML-based data validation engine presented in this article addresses each of these concerns and yields the following benefits:

  • XML meta-language handles typical data validation logic and can be easily extended.
  • Easily and seamlessly integrated into any application that needs to validate data input. This is especially valuable for applications that need to support multi-channel clients (browsers, mobile devices, voice, and Web services) from a common codebase.
  • XML-based repository makes modifying validation rules and criteria easy and intuitive.
  • Localized validation rules and error messages simplify internationalization.
  • XML-based repository is lightweight and efficient.


Jing Xue is a Senior Software Engineer at Cysive, Inc. based in Reston, VA. Jing has worked with Java and XML since joining Cysive in 1999 and is currently engaged in development of Cymbio, Cysive's J2EE-based multi-channel product.
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