| > Home |
Validator Javascript Extension |
![]() |
I've written this extension for Commons Validator which dynamically renders the javascript for Form validation. I believe it has significant improvements over the Struts validation javascript rendering (see Features below) as well as abstracting out the rendering into an environment agnostic tool.
The best way to look at this is download and deploy the webapp - it lets you try it out and browse through the source code and generated javascript (runs on Servlet 2.2 or above). Also the java and javascript source code is included in the download.
These downloads are distributed under the Apache 2.0 License.
The main components are the following:
1. ScriptRenderer
Renders the appropriate javascript for a Form's validation to a StringBuffer. This class is
most of this proposal. Its agnostic about its environment (e.g. doesn't know anything about Servlets, jsp
or Struts) and can be easily plugged into any environment. Its an abstract class that just needs two
methods implemented - mainly the getMessage() method to convert message keys into actual messages.
In the download there is a new version of the Struts JavascriptValidatorTag which uses a Struts implementation
of the ScriptRenderer.
2. FunctionRenderer.java
A FunctionRenderer understands how to render a call to a Validator's javascript validation method. Each Validator will need an associated FunctionRenderer, however having said that there is a GenericFunctionRenderer which can be used for all but one of the existing javascript validations - the only custom FunctionRenderer needed for the existing javascript validators was for the Date Validator.
3. Javascript Validators
I have modified the existing javascript validators. In summary they are smaller, simpler and have more appropriate method signatures. A summary of the difference to the existing ones are...
validateRequired(field)
Array of fields - they
just validate a single field.
As well as the general re-factoring of the methods mentioned above, I have also re-written the Date Validation javascript.
I think this is worth doing based on the addtional features alone, but for it would also be good to expand the use of Commons Validator.
I believe that this enhacement will firstly open up Commons Javascript Validations to additional developers/software and secondly opens it up to be used in different ways.
Additional Developers/Software
I believe that outside of Struts, the current Commons Validator Javascript implementations are un-useable. This feature would open up Commons Validator to be used in any number of areas. I have no knowledge of them, but what springs to mind are other web frameworks and (possibly) Java Server Faces implementations. Additionally, it would also open it up to other View Presentation technologies - such as Velocity.
Additional Uses
I have created example implementations of the existing Struts html input tags that
automatically generate validation javascript for the onchange event and
example use of these tags is shown in the download webapp.
Another area of opportunity is in the how errors are handled - the existing Javascript validators
simply set the focus on the first field in error and use a Javascript alert()
to display the messages. This implementation does the same. However in this implementation the error
handling has been put into separate methods that are included in the static ValidatorUtilities.js
script (see the processError() and
handleFieldError()/handleFormErrors() methods) - these could be easily customized
and replaced with more impressive features.
This new version I believe will resolve the following issues...
This new version may (I only did a quick scan) help resolving the following issues...