0

i have this Class

public Class Employee
{
    String name;
    List<Address> listOfAddress;
}

public Class Address
{
    String location;
    String streetName;
}

in my JSP page i have filled like this

 <s:textfield id="streetName" name="listOfAddress[%{listOfAddress.size()}].streetName" size="20" maxlength="24" cssStyle="width: 100px" />

each time i submit the page an object of time Address is added to the list therefore the size will increase by one. when i view the source HTML of the previous textField, its name looks like this listOfAddress[0].streetName , if i submit the JSP page after succefull addition, it will return to the same page and the name of this textfield will be listOfAddress[1].streetName if you view its HTML source

and like this i can add as many addresses as i want to same Employee object.

so far everything is OK. the problem is when i want to validate this field ican't because it is dynamic if i put this validation it will validate it the first time only.

     <field name="listOfAddress[0].streetName">
        <field-validator type="requiredstring" short-circuit="true">
           <param name="trim">true</param>
           <message>sorry this field is required field</message>
       </field-validator>
    </field> 

what i want is to make the index of the list "listOfAddress" dymanic according to the size of the list.

i don't know how to pass it dynamicall from the jsp

can i do something like this ?

    <field name="listOfAddress[**${dynamic index value}**].streetName">
        <field-validator type="requiredstring" short-circuit="true">
           <param name="trim">true</param>

           **<param name="myIndex">${dynamic index value}</param>**

           <message>sorry this field is required field</message>
       </field-validator>
    </field> 

or pass the dynamic value to a custom validator ?

please help me, how to validate the list when the index is dynamic

user1512999
  • 237
  • 2
  • 6
  • 19
  • I'm confused, that you got `listOfAddress[%{listOfAddress.size()}].streetName`, if there are 2 items in your list, and how could you access `listOfAddress[2]` – Jaiwo99 Sep 17 '12 at 14:01
  • the aim of this listOfAddress[%{listOfAddress.size()}].streetName is to let ModelDriven map the fields of List listOfAddress dircetly, look at the 2 classes at the beginning of the question, everything is ok, the problem is how to validate it when i have a dynamic index for the list – user1512999 Sep 18 '12 at 05:56
  • in my JSP page i have the fields of the Employee Class to be added and at the buttom of the page i use ajax to add many addresses to same employee. so they appear as grid at the buttom of the page under the information of the employee – user1512999 Sep 18 '12 at 05:58

1 Answers1

0

Use visitor validator. See http://struts.apache.org/2.x/docs/using-visitor-field-validator.html.

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143