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