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 2

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

A Java/XML Server-Side Data Validation Engine

Capabilities and Architecture
Our Java/XML-based data validation application will include the following:

  • Definition of a form and its fields (through an XML configuration repository or through direct manipulation of form and field definitions)
  • Instantiation of form instances based on a form definition
  • Update of forms from text-based input sources
  • Validation of input criteria, including field-specific error message generation
  • Support for field attributes beyond validation criteria (such as field labels)
  • Support for localization of all field validation criteria and attributes
  • Support for customized field-specific error messages, including localization of message text
  • User-customizable field types (definitions)
The actual definition of a form that can be submitted is maintained in a form definition repository XML file that describes all the forms in an application. For each field in each form the following properties can be specified:

  • The expected type of this particular field—plain text, integer, decimal, Boolean, choice, multi-choice, etc.
  • A key to identify this field
  • Whether this field is required (i.e., user input is mandatory for this field)
  • Type-specific criteria such as minimum/maximum length for text fields, inclusively/exclusively maximum/minimum boundary values for numerical fields, valid date range for date fields, etc.
  • Error messages that can be displayed when certain rules are violated.
Automation of the handling of forms can then be achieved by programmatically invoking the validation engine from the servlet code that handles the form submission. If any of the data is invalid, the program responds with the specific error message associated with that field. A set of error message expansion keywords is defined to dynamically generate error messages based on field constraints.

A Meta-language for Validation Rules
Table 1 shows the XML definitions for the text field types in the application. The other field types (not included) have similar attribute sets and semantic structure:

Text Field Definition
Description A simple text field with length validations.
XML Element <text-field>
Parsed Value java.lang.String
Class com.cysive.framework.forms.TextDefinition
Field Element Attributes
key The unique ID of this field (only needs to be unique within a form).
required If "true", the field is required. If trim is also specified, the field must contain something other than whitespace. If "localized", the resource property keyed by "required" must be "true" for the field to be required, all other values are considered false.
trim If "true", the field will be trimmed of all leading and trailing whitespace prior to validation. If "localized", the resource property keyed by "trim" must be "true" for the field to be required, all other values are considered false.
Nested Elements
<label> The label (name) for the field.
<default> The default text for the field. It is considered valid to supply a default that is invalid according to the validation criteria.
<min-length> The minimum length in characters for this field. Must be >= 1 and <= max-length. Values outside of integer range are wrapped.
<max-length> The minimum length in characters for this field. Must be >= 1 and >= min-length. Values outside of integer range are wrapped.
Error Message Keys
missing The field is required and no value has been supplied.
length-out-of-range-min-max The number of characters in the field is out of range and both min-length and max-length have been specified.
length-out-of-range-min The number of characters in the field is out of range and only min- length has been specified.
length-out-of-range-max The number of characters in the field is out of range and only max- length has been specified.
Error Expansion Keywords
${label} The field's label.
${max-length} The maximum length specified for the field.
${min-length} The minimum length specified for the field.


The DTD file for the form definition XML files can be found at http://cymbio.cysive.com/dtds/formdef_1_0.dtd or email the author at jxue@cysive.com.


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