Last updated April 10, 2011 16:30, by sdouglass
Feedicon  

How It Works

Generating The Validation JavaScript

The validate taglib uses the command object name from the Spring MVC form tag to retrieve the command object from the page context. It then retrieves the JSR-303 Validator instance from the Spring Web application context. The taglib uses the Validator and the command obejct's class to retrieve the validation metadata for the command object. This metadata is stored as a list of objects containing the object field name, the validation annotation, and the annotation's attributes. This metadata is translated into JavaScript that can be run to validate an HTML form.

Example

To illustrate the JavaScript output, consider the following Java bean:

 public class User {
  @NotEmpty(message = "Please provide an e-mail address")
  @Email(message = "Please provide a valid e-mail address")
  private String email;
  
  @NotEmpty(message = "Please provide a password")
  @Length(min = 8, max = 12, message = "Password must be 8-12 characters long")
  private String password;
  
  /* rest of code omitted */
 }

The validate tag would produce JavaScript like the following (white space and comments have been added to illustrate the different parts of the JavaScript):

 new JSR303JSValidator(
   'user' // the command object's name
   true, // boolean indicating if the validator should be configured to 
         // handle the onsubmit event of the form
   {}, // an optional JSON object taken from the body of the validate tag
   new Array( // an array of validation rules, one per validation annotation
     new JSR303JSValidator.Rule(
       'email', // the validated field name
       'NotEmpty', // short class name of the annotation, used as the name
                    // of the JavaScript function that performs the validation
       { // the attributes set on the annotation
         'message' : 'Please provide an e-mail address' 
       } 
     ),
     new JSR303JSValidator.Rule(
       'email',
       'Email',
       {'message' : 'Please provide a valid e-mail address'}
     ),
     new JSR303JSValidator.Rule(
       'password', 
       'NotEmpty', 
       { 'message' : 'Please provide a password' } 
     ),
     new JSR303JSValidator.Rule(
       'password',
       'Length',
       {'min' : '8', 'max' : '12', 'message' : 'Password must be 8-12 characters long'}
     )
   )
 )

JavaScript Validation Functions

Each validation Annotation has a corresponding JavaScript validation function. For example, the following function performs the @Length validation:

  Length: function(value, params) {
    var valid = true;
    if (value) {
      var valueLength = value.toString().length;
      if (params.min && valueLength < params.min) {
        valid = false;
      }
      if (valid && params.max && valueLength > params.max) {
        valid = false;
      }
    }
    return valid;
  },

The JavaScript functions are passed the field value and the annotation attributes (in the "params" argument). They are also passed the field name and the JSON object provided in the body of the tag. Functions can use the field name and the JSON object to retrieve additional configuration information, such as date formats. For example:

  Future: function(value, params, fieldName, config) {
    var valid = true;
    if (value) {
      var dateFormat = (
        config[fieldName] && config[fieldName].dateFormat ? 
          config[fieldName].dateFormat : 
          JSR303JSValidator.DateParser.defaultFormat
      );
 ...
    return valid;
  },


  • Mysql
  • Glassfish
  • Jruby
  • Rails
  • Nblogo
Terms of Use; Privacy Policy;
© 2014, Oracle Corporation and/or its affiliates
(revision 20160708.bf2ac18)
 
 
Close
loading
Please Confirm
Close