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.
by Jing Xue
Apr 1, 2002
Page 2 of 4
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 fieldplain 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
A simple text field with length validations.
Field Element Attributes
The unique ID of this field (only needs to be unique within a form).
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.
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.
The label (name) for the field.
The default text for the field. It is considered valid to supply a default that is invalid according to the validation criteria.
The minimum length in characters for this field. Must be >= 1 and <= max-length. Values outside of integer range are wrapped.
The minimum length in characters for this field. Must be >= 1 and >= min-length. Values outside of integer range are wrapped.
Error Message Keys
The field is required and no value has been supplied.
The number of characters in the field is out of range and both min-length and max-length have been specified.
The number of characters in the field is out of range and only min- length has been specified.
The number of characters in the field is out of range and only max- length has been specified.