function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
moverdorfmoverdorf 

Hopefully An Easy Question...

My question is, if I have input fields that are named with the same ID multiple times, what is the syntax to pass the inputted value into my APex class:

 

For example: I have the following that is in a loop for the number of financial accounts I returned in a query. So I may have ONE or MANY of these

     my loop...

                 <tr>
                    <td align="right">From Date:(mm/dd/yyyy)</td>
                    <td><apex:inputText id="fromDate" value="{!fromDate}"/></td>  
                    
                </tr>  
                <tr>
                    <td align="right">To Date:(mm/dd/yyyy)</td>
                    <td><apex:inputText id="toDate" value="{!toDate}"/></td>  
                    
                </tr> 

 end loop...

 

In other languages when I have had this situation I would have a field named fromDate, which would be an array of values, and if I wanted the 5th one from my input form it would be fromDate[5]. How is this done in Apex/VisualForce ?

 

If I were to name them uniquely like fromDate1, toDate1, fromDate2, toDate2, then I would have to create getter/setter for every one in the loop! There has to be an easier way right?

 

Thanks guys and gals.

Best Answer chosen by Admin (Salesforce Developers) 
Dev@Force.ax647Dev@Force.ax647

Ok, This is how your apex will be

 


APEX:
public List<DateWrapper> listDateWrapper{get;set;}
public void init()
{
for(Integer i=0;i<10;i++)
{
listDateWrapper.add(new DateWrapper());
}
}
public class DateWrapper()
{
public Date fromDate{get;set;}
public Date toDate{get;set;}
}

Visualforce:
<apex:repeat value="{!listDateWrapper}" var="dw">
From:<apex:inputText value="{!dw.fromDate}"/>
From:<apex:inputText value="{!dw.toDate}"/>
</apex:repeat>

 

 

 

 

 

All Answers

Dev@Force.ax647Dev@Force.ax647

Use Wrapper class and create a list and bind in your loop

http://wiki.developerforce.com/page/Wrapper_Class

moverdorfmoverdorf

Thanks, I read the link but I am new to Apex and could use a little more help with the syntax .

 

Would there be a list  for the fromDate and a list for the toDate?

 

Could you give me an example of what the wrapper class might look like for my example. I am a little confused about the List declaration in the Apex wrapper class.

 

Thanks so much for your help, I greatly appreciate it.

Dev@Force.ax647Dev@Force.ax647

Ok, This is how your apex will be

 


APEX:
public List<DateWrapper> listDateWrapper{get;set;}
public void init()
{
for(Integer i=0;i<10;i++)
{
listDateWrapper.add(new DateWrapper());
}
}
public class DateWrapper()
{
public Date fromDate{get;set;}
public Date toDate{get;set;}
}

Visualforce:
<apex:repeat value="{!listDateWrapper}" var="dw">
From:<apex:inputText value="{!dw.fromDate}"/>
From:<apex:inputText value="{!dw.toDate}"/>
</apex:repeat>

 

 

 

 

 

This was selected as the best answer
moverdorfmoverdorf

Thank you so much for all your effort, I learned a lot from you today and I really appreciate it.