Recipe8.3.Validating Dependent Fields in Struts 1.1


Recipe 8.3. Validating Dependent Fields in Struts 1.1

Problem

You are using Struts 1.1 and you want to validate a field based on the value of another related field.

Solution

Use the requiredif validator. The field element, in this snippet from a validation.xml file, indicates that the zipCode field is required if the city or state field is null:

<!-- zipCode is required if city is null or state is null --> <field property="zipCode" depends="requiredif">     <arg key="prompt.zipCode"/>     <var>         <var-name>field[0]</var-name>         <var-value>city</var-value>     </var>     <var>         <var-name>fieldTest[0]</var-name>         <var-value>NULL</var-value>     </var>     <var>         <var-name>field[1]</var-name>         <var-value>state</var-value>     </var>     <var>         <var-name>fieldTest[1]</var-name>         <var-value>NULL</var-value>     </var>     <var>         <var-name>fieldJoin</var-name>         <var-value>OR</var-value>     </var> </field>

Discussion

The Struts Validator has always worked well for single-field validations. Cross-field validation, that is validating two or more dependent fields, wasn't supported by the Validator until Struts 1.1. The requiredif validation rule was introduced at that time to address the problem. Interestingly enough, the requiredif rule lifespan will be short. It is being deprecated in Struts 1.2 and is being replaced by the validwhen rule, discussed in Recipe 8.4.

The validation shown in the Solution would be used on a form where you were retrieving a user's address. If users specify a zip code, then they can omit the city and state; these values would be looked up based on the zip code. On the other hand, if the city or state is not specified, then the zip code is required.

The requiredif validator can't be used for client-side validation, only server-side validation.


Before Struts 1.1, cross-field validations like this had to be hand coded in the ActionForm or Action. With Struts 1.1, you can use the requiredif rule to indicate that a field is required if the value of other fields meet certain criteria. The criteria are defined by specifying variable names and values for the rule using the var element.

Each criterion for a dependent field is identified by an index. In the Solution, the indices are used to create two criterion sets for the dependent fields; city and state. The index is used to group together a field name, test type, and value. These values combine to represent an expression. The corresponding var-value for the field[i] variable refers to a form property:

<var>     <var-name>field[0]</var-name>     <var-value>property name</var-value> </var>

The fieldTest[i] variable defines the type of test:

<var>     <var-name>fieldTest[i]</var-name>     <var-value>test type</var-value> </var>

The following test types are accepted:


NULL

The field must be null or an empty string.


NOTNULL

The field must not be null or an empty string.


EQUAL

The field value must be equal to a specific value.

If the field test is NULL or NOTNULL, then the criterion for that index is complete. If the field test is EQUAL, the fieldValue[i] variable contains the literal value to compare against:

<var>     <var-name>fieldValue[i]</var-name>     <var-value>literal value</var-value> </var>

If the property is numeric, the literal value will be converted to a number; otherwise, it is treated as literal text for a String comparison.

If more than one field is specified for a requiredif validationthere is more than one criterionyou can logically connect the criterion using the fieldJoin variable:

<var>     <var-name>fieldJoin</var-name>     <var-value>logical operator</var-value> </var>

Valid values for the logical operator are AND and OR. Using a logical AND indicates that the validation passes if all the requiredif field criteria are true. A value of OR indicates that the validation passes if any one of the field criteria is true.

If fieldJoin isn't specified, then AND will be assumed.


See Also

If you are using Struts 1.2, the requiredif validator is deprecated. Instead, use the validwhen validator described in Recipe 8.4.

The latest documentation on requiredif can be found in the Struts Validator Guide, available online at http://struts.apache.org/userGuide/dev_validator.html.



    Jakarta Struts Cookbook
    Jakarta Struts Cookbook
    ISBN: 059600771X
    EAN: 2147483647
    Year: 2005
    Pages: 200

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net