8.11. Validating a Wizard Form
Problem
You need to perform validations on a form shared across pages as part of a wizard-style interface.
Solution
Use the
Validator's built-in
support for wizards via the page attribute. Example 8-16 configures the Validator use of this
attribute. Validations will be performed on fields where the
page attribute value is less than or equal to the
page property of the
ActionForm.
Example 8-16. Use of the Validator page attribute
<form name="WizardForm">
<field property="username" page="1"
depends="required">
<arg key="prompt.username"/>
</field>
<field property="password" page="2"
depends="required">
<arg key="prompt.password"/>
</field>
<field property="ssn" page="3"
depends="required,mask">
<arg key="prompt.ssn"/>
<var>
<var-name>mask</var-name>
<var-value>^\d{3}-\d{2}-\d{4}$</var-value>
</var>
</field>
</form>If the form used for the wizard uses the
DynaValidatorForm
(or one of its subclasses), add a form-property
with the name of page. The property must be of
type java.lang.Integer:
<form-bean name="WizardForm"
type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="username" type="java.lang.String"/>
<form-property name="password" type="java.lang.String"/>
<form-property name="ssn" type="java.lang.String"/>
<form-property name="page" type="java.lang.Integer"/>
</form-bean>For conventional ActionForms that subclass
ValidatorForm
(or one of its subclasses), no changes are necessary. The
ValidatorForm already provides a
page property. ...