8.5. Validating an Indexed Property

Problem

You want to validate a field that is a nested property of an object in an array or Collection.

Solution

Set the indexedListProperty attribute for the field element to the name of the property that returns the array or Collection. The value of the property attribute will be interpreted as the name of a nested property of an element within the Collection. If you're using Struts 1.2, you can reference the indexed property in a test for a validwhen rule. Example 8-5 shows a complete form element from validation.xml for a form containing the array property "orders."

Example 8-5. Validations for an indexed list property (partial)

<form name="IndexedListForm"> <field property="partNumber" indexedListProperty="orders" depends="minlength"> <arg position="0" key="prompt.partNumber"/> <arg position="1" key="${var:minlength}" resource="false"/> <var> <var-name>minlength</var-name> <var-value>5</var-value> </var> </field> <field property="quantity" indexedListProperty="orders" depends="intRange,validwhen"> <arg position="0" key="prompt.quantity"/> <arg position="1" key="${var:min}" resource="false"/> <arg position="2" key="${var:max}" resource="false"/> <msg name="validwhen" key="error.quantity.invalid"/> <var> <var-name>min</var-name> <var-value>5</var-value> </var> <var> <var-name>max</var-name> <var-value>20</var-value> </var> <var> <var-name>test</var-name> <var-value> (((orders[].partNumber != null) and (*this* != null)) or ((orders[].partNumber == ...

Get Jakarta Struts Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.