Validate Properties in a List of Object

I use the Struts validation.xml to make sure that users do not leave any of the text fields in the web page blank.

The complications come. All text fields are properties of an object. The "form" that is associated with the action mapping has only the getter and setter of that object 'selectedATSUsers'. To display my web page, I iterate through a List of that object:

<logic:iterate id="selectedATSUsers" name="orgUnitRoleForm"

type="......user.ATSTFUser">

First Name: <html:text name="selectedATSUsers" property="firstName" indexed="true">

<bean:write name="selectedATSUsers" property="firstName" />

</html:text>

Last Name: <html:text name="selectedATSUsers" property="lastName" indexed="true">

<bean:write name="selectedATSUsers" property="lastName" />

</html:text>

</logic:iterate>

The orgUnitRoleForm is in the action mapping. But, firstName and lastName are not properties in the orgUnitRoleForm.

Normally, I can specify something like:

<field property="firstName"

depends="required">

<arg0 key="xyz" />

</field>

for validation.

What to do to validate firstName that is inside an object?

[1602 byte] By [jiapei_jena] at [2007-10-3 1:32:00]
# 1

Check the struts documentation on the validator:

Specifically under "Designing Complex Validations with validWhen"

http://struts.apache.org/1.2.x/userGuide/dev_validator.html

It shows an example of an indexed property, where it uses empty square brackets to refer to one specific entry in the indexed property.

Maybe this will work:

<field property="selectedATSUsers[].firstName"

depends="required">

<arg0 key="xyz" />

</field>

Sorry don't have time to test it, but fingers crossed...

evnafetsa at 2007-7-14 18:29:55 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

After a quick [url http://www.google.com/search?hl=en&q=struts+validator+indexed+properties&meta=]google[/url], found another link:

http://www.strutskickstart.com/

It seems to suggest the following syntax:

<field property="firstName" indexedProperty="selectedATSUsers"

depends="required">

<arg0 key="xyz" />

</field>

Cheers,

evnafets

evnafetsa at 2007-7-14 18:29:55 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Thanks a lot, evnafets.

Most of the questions that I have ever posted on the java.sun.com forums have been anwsered by you. Thank you for spending so much time helping and supporting me.

Here are my testing results:

<field property="selectedATSUsers[].firstName"

depends="required">

<arg0 key="xyz" />

</field>

does not seem to invalidate the fields that are left blank. Although the code accepts users newly entered info. and saves it properly.

<field property="firstName" indexedProperty="selectedATSUsers"

depends="required">

<arg0 key="xyz" />

</field>

seems to do the validation too much. For example, I iterate through three selected users: A, B, and C

selectedATSUsers A:

firstName (leave it blank)

lastName (filled out)

id (filled out)

selectedATSUsers B:

firstName (filled out)

lastName (leave it blank)

id (filled out)

selectedATSUsers C:

firstName (filled out)

lastName (filled out)

id (filled out)

The validation says:

first name is required.

last name is required.

id is required.

But the id field for all three users is not left blank.

jiapei_jena at 2007-7-14 18:29:55 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Thank you, evnafets, for helping me.

I have found the answer to my question. I would like to share the solution with everybody.

The attribute to be inserted is "indexedLinkProperty" instead of "indexedProperty". Please see below:

<field property="firstName" indexedLinkProperty="selectedATSUsers"

depends="required">

<arg0 key="xyz" />

</field>

jiapei_jena at 2007-7-14 18:29:55 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...