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

JSR303JS for Spring 3 MVC Wiki

This library provides client side validation of an HTML form based on JSR-303 and Hibernate Validator annotations, integrated with Spring MVC. The library provides a JavaScript validation code base that handles basic interaction with HTML forms, as well as JavaScript functions implementing the validation annotations supported by Hibernate Validator (including those not from the JSR-303 spec). This JavaScript code base can be included in a page by using a provided taglib or by extracting the JavaScript file from the jar and including it using a <script> tag. Once this code base has been included in a page, a second taglib is used to generate the JavaScript code for validating an HTML form. You can also provide a JSON object in the body of the tag to specify additional configuration information.

This library is derived from the deprecated Spring Modules Validation module, specifically the JavaScript implementation of the Valang validation language. The taglibs and JavaScript in this library are slightly modified versions of the taglibs and JavaScript from that code. Rather than loading Valang validation rules and translating them into JavaScript this library instead loads validation annotations and translates them into JavaScript. Some of the JavaScript validation functions are derived from the Hibernate Validator implementation code to make sure the client side validation is consistent with the server side validation (specifically the regular expression used to validate e-mail addresses).

For now you can access the source code through the Subversion repository. There is a demo webapp project you can download as well.

SVN checkout URLs:

https://svn.kenai.com/svn/jsr303js~svn-repository/jsr303js/trunk

https://svn.kenai.com/svn/jsr303js~svn-repository/jsr303jsdemo/trunk

Browse SVN

More Info

Quick Start Guide

1. Import the JavaScript code base

Use the codebase taglib

You can include the JavaScript code base using the codebase taglib as in this example:

 ...
 <%@ taglib prefix="jsr303js" uri="http://kenai.com/projects/jsr303js/" %>
 ...
     <jsr303js:codebase/>
 ...

-OR-

Use the JavaScript file

You can extract the JavaScript code base file from the jar with the following command:

 jar xvf jsr303js-1.0-SNAPSHOT.jar jsr303js-codebase.js

You can then place the extracted JavaScript file somewhere in your webapp and include it using a <script> tag as in this example:

 <script type="text/javascript" src="js/jsr303js_codebase.js"></script>

NOTE: The codebase contains a fair amount of JavaScript, so it is recommended to extract the JavaScript file and serve it separately so that it can be cached by users' browsers.

2. Use the validate taglib

Now to add validation to your form you must use the validate taglib as in this example:

 ...
 <body>
 ...
   <form:form id="testModelBean" commandName="testModelBean">
 ...
  <jsr303js:validate commandName="testModelBean"/>
 ...

3.Displaying validation errors

By default validation errors will be shown in a JavaScript alert.

You can instead have the errors displayed on the page. You can display all errors in an element with an id of 'global_errors', e.g.:

 <div id="global_errors"></div>

Errors will be added to the element as list items in an unordered list. You can also display errors for a specific field in an element with an id composed of the field name plus the suffix '_error', e.g.:

 <div id="email_error"></div>

You can use a global error element, individual field elements, or both. Any error not displayed in an individual field element will be displayed in the global error element.

The global error element id and individual field element id suffix can be customized using the 'globalErrorsId' and 'fieldErrorsIdSuffix' attributes of the codebase taglib.

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