Struts Validator
 Validation Framework for Struts
Home
Installation
Overview
JSP Tags
Javadoc
To Do List
Revision Info
Downloads
Contact Information

JSP Tags

Javascript Validator TagGenerates Javascript based on validation.xml.
Errors TagIterates through each error.
Errors Exist TagConditionally displays body if there are any errors in scope.
Messages TagIterates through each message.
Messages Exist TagConditionally displays body if there are any messages in scope.

Javascript Validator Tag

Javascript is generated to match the validation that is performed server side if the JavascriptValidatorTag is set correctly and the form onSubmit value must be added manually. The pluggable validator must have a Javascript validation method specified for its javascript.

When the tag is generating the client side Javascript generation, it looks for a value in the validator's javascript attribute and generates an object for each pluggable validator that the supplied method can use to validate the form. The Javascript object is named after the validator's name attribute. Currently a simple sort is performed to create the order that the different Javascript validation methods are called in and they are each joined by the logical AND (&&).
    return validateRequired(form) && validateRange(form);

The Javascript object's variable that corresponds to a field on the form is an Array. The first element is the name of the field, the second element is the internationalized error message with correct parameter replacements, and the third element is a Javascript Function object that has all of the variables associated with this field. Below is pseudo Javascript code to show how to loop through the auto-generated Javascript object and retrieve a variable value.

    function validateRange () {
       oRange = new range();
       for (x in oRange) {
          if (form[oRange[x][0]].type == 'text' || form[oRange[x][0]].type == 'textarea') {
             var iMin = parseInt(oRange[x][2]("min"));
             alert (iMin + " is the value for the min variable.");
          }
       }
   }

    function range () {
       this.aa = new Array("integer", "Integer Field is not in the range 10 through 20.", new Function ("varName", "this.max='20'; this.min='10'; return this[varName];"));
   }

Error messages may look odd if there is html formatting mixed in with some of them. This will probably be removed in Struts 1.1 and some kind of message/error iterator will take it's place. I have made a simple errors tag that iterates over the error messages so HTML formatting does not need to be in the message resources.

If the name attribute in the action mapping is registrationForm, then this would be added to html:form.
   onsubmit="return validateRegistrationForm(this);"

    <validator:javascript formName="registrationForm"/>

OR

If you want to specify the Javascript method name, to be used.
    onsubmit="return subValidate(this);"

    <validator:javascript formName="registrationForm" methodName="subValidate"/>


Have a Submit Button in a Form not Perform Validation
If you want to turn off the validation for a specific submit button like the html:cancel tag, you can set the bCancel variable to true for the JavaScript validation not to execute and bCancel to false for it to execute.
    <html:submit onclick="bCancel=false;">
       <bean:message key="button.save"/>
    </html:submit>

    <html:cancel onclick="bCancel=true;">
       <bean:message key="button.cancel"/>
    </html:cancel>


Creating a Separate Page for Static JavaScript

If you want to just generate the dynamic JavaScript on you form page and have a separate page with the static JavaScript to take advantage of browser caching you can use the dynamicJavascript and staticJavascript attributes to the JavascriptValidatorTag. You can turn each attribute's generation of JavaScript on and off by putting in true or false (they default to true). Reference jsType.jsp and staticJavascript.jsp in the main example webapp for a working example.

In your form page:
    <validator:javascript formName="typeForm" dynamicJavascript="true" staticJavascript="false"/>
    <script language="Javascript1.1" src="staticJavascript.jsp"></script>

    This wouldn't work right in IE 5.0 when I made the source an attribute of the tag.

In your static JavaScript page.
    <%@ page contentType="application/x-javascript" %>
    <validator:javascript dynamicJavascript="false" staticJavascript="true"/>

    Setting the content type is supposed to address a bug in Netscape according to an online resource.