Validator - Enabling Client-Side Validations
(Page 6 of 9 )
In addition to providing a framework for simplifying server-side form data validations, Validator provides an easy-to-use mechanism for performing client-side validations. Each validation routine defined in the validator-rules.xml file optionally specifies JavaScript code that can be run in the browser (client side) to perform the same validations that take place on the server side. When run on the client side, the validations will not allow the form to be submitted until they have all passed.
In addition to providing a framework for simplifying server-side form data validations, Validator provides an easy-to-use mechanism for performing client-side validations. Each validation routine defined in the file optionally specifies JavaScript code that can be run in the browser (client side) to perform the same validations that take place on the server side. When run on the client side, the validations will not allow the form to be submitted until they have all passed.
To enable client-side validation, you have to place the HTML Tag Library’s javascript tag in each JSP for which you want validation performed, as shown here:
<html:javascript formName="logonForm"/>
The javascript tag requires that you use the formName attribute to specify the name of a <form> definition from the validation.xml file, as shown here, for which you want validations performed:
<form name="logonForm">
<field property="username" depends="required">
<arg0 key="prompt.username"/>
</field>
<field property="password" depends="required">
<arg0 key="prompt.password"/>
</field>
</form>All the validations that you have specified for the <form> definition to run on the server side will be run on the client side.
------------Creating Custom Validations
Validator comes packaged with several useful validations that will suit most validation scenarios; however, your application may require a specific validation that is not provided by the framework. For this situation, Validator provides a simple interface for creating your own custom validations that can easily be plugged into the framework. Following is a list of the steps you need to take to create your own custom validation:
- Create a new validation method.
- Add a new validation rule to the validator-rules.xml file.
- Add new validation definitions to the validation.xml file.
- Add messages to the ApplicationResources.properties file.
The following sections walk through each step of the process in detail, enabling you to create a custom validation based on the Social Security Number validation used in the example Mini HR application in Chapter 2. Remember that the Social Security Number validation in Chapter 2 was defined inside of the SearchForm Form Bean class. Creating a custom validation for social security numbers enables the validation code to be reused and to be used declaratively instead of being hard-coded in each Form Bean that wants to use it.
Creating a Validation Method
The first step in creating a custom validation is to create a validation method that can be called by the Validator framework. Typically, all of an application’s methods for custom validations are grouped into a class of their own, as is the case for the example in this section. However, you can place the method in any class. Your validation method needs to have the following signature:
public static boolean validateSsNum(java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionErrors,
javax.servlet.http.HttpServletRequest);
Of course, the name of your method will vary, but its arguments should match the types shown in the preceding example. Table 6-2 explains each of the validation method’s arguments.
Following is the custom validation code for validating social security numbers. Notice that the validateSsNum( ) method conforms to the proper method signature for custom validations.
package com.jamesholmes.minihr;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.validator.Field;
import org.apache.commons.validator.ValidatorAction;
import org.apache.commons.validator.ValidatorUtil;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.validator.Resources;
public class MiniHrValidator
{
public static boolean validateSsNum(Object bean,
ValidatorAction action,
Field field,
ActionErrors errors,
HttpServletRequest request)
{
String value =
ValidatorUtil.getValueAsString(bean, field.getProperty());
if (value == null || value.length() < 11) {
errors.add(field.getKey(),
Resources.getActionError(request, action, field));
return false;
}
for (int i = 0; i < 11; i++) {
if (i == 3 || i == 6) {
if (value.charAt(i) != '-') {
errors.add(field.getKey(),
Resources.getActionError(request, action, field));
return false;
}
} else if ("0123456789".indexOf(value.charAt(i)) == -1) {
errors.add(field.getKey(),
Resources.getActionError(request, action, field));
return false;
}
}
return true;
}
}
Argument | Description |
java.lang.Object | The Form Bean object (down casted to Object) |
| | contains the field to be validated. |
org.apache.commons.validator.ValidatorAction | The ValidatorAction object encapsulates the |
| | <validator> definition from the |
| | validator-rules.xml file for this validation routine. |
org.apache.commons.validator.Field | The Field object encapsulates the <field> |
| | definition from the validation.xml file for the |
| | field that is currently being validated. |
org.apache.struts.action.ActionErrors | The ActionErrors object stores validation error |
| | messages for the field that is currently being |
| validated. |
javax.servlet.http.HttpServletRequest | The HTTPServletRequest object encapsulates |
| | the current HTTP request. |
TABLE 6-2 The Validation Method Arguments
The validateSsNum( ) method begins by retrieving the value for the field being validated. The value is retrieved by determining the field’s name with a call to field.getProperty( ) and then looking up that field in the Form Bean with a call to ValidatorUtil.getValueAsString( ) . The getValueAsString( ) method matches the name of the field with the name of one of the Form Bean fields and then gets that field’s value. The rest of the validateSsNum( ) method performs the actual validation logic. If the validation fails for any reason, an error message will be stored in the errors ActionErrors object.
Next: Adding a New Validation Rule >>
More Java Articles
More By McGraw-Hill/Osborne
|
This article is taken from chapter six of the book The Complete Reference Struts, written by James Holmes (McGraw-Hill/Osborne, 2004; ISBN: 0072231319). Check it out at your favorite bookstore. Buy this book now.
|
|