Several JSP custom tags included within the Struts tag libraries can be used with the Validator framework. One of the tags is used to generate dynamic JavaScript based on the validation rules. The other tags are part of the core Struts framework and are used both with and without the Validator.
The tags listed in Table 11-4 are generic and can be used with or without the Validator framework, but they all come in handy when using it.
The tags in Table 11-4 allow JSP pages to detect and obtain access to messages or errors that were detected in the Struts application. These tags were discussed in more detail in Chapter 8.
The Validator framework is also capable of generating JavaScript for your Struts application using the same framework as for server-side validation. This is accomplished by using a set of JSP custom tags designed specifically for this purpose.
The Validator custom tag called JavascriptValidator is used to generate client-side validation based on a javascript attribute being present within the validator element. Before the JSP custom tag can be used, there must be a javascript element for the validation rule. The following code fragment illustrates the required validation rule that includes a javascript element:
<validator
name="required"
classname="org.apache.struts.util.StrutsValidator"
method="validateRequired"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionErrors,
javax.servlet.http.HttpServletRequest"
msg="errors.required">
<javascript><![CDATA[
function validateRequired(form) {
var bValid = true;
var focusField = null;
var i = 0;
var fields = new Array( );
oRequired = new required( );
for (x in oRequired) {
if ((form[oRequired[x][0]].type == 'text' ||
form[oRequired[x][0]].type == 'textarea' ||
form[oRequired[x][0]].type == 'select-one' ||
form[oRequired[x][0]].type == 'radio' ||
form[oRequired[x][0]].type == 'password') &&
form[oRequired[x][0]].value == '') {
if (i == 0)
focusField = form[oRequired[x][0]];
fields[i++] = oRequired[x][1];
bValid = false;
}
}
if (fields.length > 0) {
focusField.focus( );
alert(fields.join('\n'));
}
return bValid;
}]]>
</javascript>
</validator>
When the JavascriptValidator tag is included in the JSP page, the text from the javascript element is written to the JSP page to provide client-side validation. When the user submits the form, the client-side validation is executed, and any validation rules that fail present messages to the user.
You will need to include the javascript tag with the name of the ActionForm that it's going to validate against:
<html:javascript formName="checkoutForm"/>
The formName attribute is used to look up the set of validation rules to include as JavaScript in the page. You will have to add an onsubmit event handler for the form manually:
<html:form action="getPaymentInfo" onsubmit="return validateCheckoutForm(this);">
When the form is submitted, the validateCheckoutForm( ) JavaScript function will be invoked. The validation rules will be executed, and if one or more rules fail, the form will not be submitted. The javascript tag generates a function with the name validateXXX( ), where XXX is the name of the ActionForm. Thus, if your ActionForm is called checkoutForm, the javascript tag will create a JavaScript function called validateCheckoutForm( ) that executes the validation logic. This is why the onsubmit( ) event handler called the validateCheckoutForm( ) function.