Home

This Chapter
-Chapter 5: The Validator Plugin
-The Default Validators
-Using Validator
-Understanding Validation Files
-Using Client Side Validation
-Using DynaValidatorForm
-Mixing Validator with Custom Validation
-Declaring Action-Based Rules
-Writing Custom Validators
-Custom Validator with Client Side Validation
-Using validwhen
-Summary

Table of Contents
-Introduction
-Chapter 1: Model 2 and Struts
-Chapter 2: Input Validation with Action Forms
-Chapter 3: The HTML Tag Library
-Chapter 4: Input Validation and Data Conversion
-Chapter 5: The Validator Plugin
-Chapter 6: The Expression Language
-Chapter 7: JSTL
-Chapter 8: The Bean Tag Library
-Chapter 9: The Logic Tag Library
-Chapter 10: Struts-EL, Nested, selectLabel
-Chapter 11: Message Handling and Internationalization
-Chapter 12: The Tiles Framework
-Chapter 13: Securing Struts Applications
-Chapter 14: The Config Object
-Chapter 15: The Persistence Layer
-Chapter 16: Object Caching
-Chapter 17: File Upload and File Download
-Chapter 18: Paging and Sorting
-Chapter 19: Preventing Double Submits
-Chapter 20: Early HttpSession Invalidation
-Chapter 21: Decorating Request Objects
-Chapter 22: How Struts Works

Previous
Next

 

Using validwhen

The most complex default validator that comes with the Validator plugin is validwhen. You use this validator to validate a field against another field. For example, when prompting the user to enter a new password, you may want the password to be entered twice to make sure there is no mistyping. Or, you may require that a field is required only if another field is not empty.

The app05h application modifies app05f to use validwhen. The HTML form contains two password elements. Validation is successful only if the values of both password elements match.

As usual, you write your validation rules using the form element. The validation rules in app05h has two fields, password and password2. The validation of the fields is defined in the validation.xml file in Listing 5.16.

Listing 5.16: The validation rules for app05h.

<form name="passwordForm">
  <field property="password" depends="securepassword">
    <arg key="error.password" position="0"/>
    <var>
      <var-name>minLength</var-name>
      <var-value>5</var-value>
    </var>
  </field>
  <field property="password2" depends="validwhen">
    <arg key="error.confirmPassword" position="0"/>
    <var>
      <var-name>test</var-name>
      <var-value>(password == *this*)</var-value>
    </var>
  </field>
</form>

The password field uses the securepassword validator, discussed in app05f and app05g. The password2 field is for the user to confirm the password they are creating. The value must be the same as the value of password. For this, the validwhen validator is used. Its test argument is given this value.

(password == *this*)

Which means that the validation of validwhen is successful if the value of the password field is the same as this value. *this* refers to the value of the current field.

You can run the app05h application by invoking the following URL:

http://localhost:8080/app05h/displayCreatePasswordForm.do

Previous
Next