ZF-3782: Zend Form not allowing multiple array elements with the same name.

Description

I need to add multiple select elements (also a few text ones) with the same name, provide[] and price[], to my form. I initially tried to just add it like any regular select but zend_form removed my brackets from the select name and only showed the last select element. So I added isArray to get my brackets I specified back and multiple as false option to the select elements to get rid of it trying to make my single select into a multi. But still it will only renders the last select from the loop. If I specify a unique name for all of them, it renders as it should but it destroys my ability to easily loop through the data when I process it after it is submitted.

Here is what I am using to make this part of the form:

foreach ($items_providers as $ip) {

$form->addElement('select', 'provide[]', array(

    'value' => $ip['providers_id'], 

    'isArray' => true,

    'multiple' => false,

    'MultiOptions' => $providers_options)

);

$form->addElement('text', 'price[]', array(

    'value' => $ip['price'],

    'isArray' => true)

);

//there are more text elements but I stopped here

}

In the end I need something like this rendered:

<option value="" label=""></option>

<option value="2" label="Test1">Test1</option>

<option value="7" label="Test2">Test2</option>

<option value="" label=""></option>

<option value="2" label="Test1">Test1</option>

<option value="7" label="Test2">Test2</option>

<option value="" label=""></option>

<option value="2" label="Test1">Test1</option>

<option value="7" label="Test2">Test2</option>

etc... All I get is one select and one text box when I should get one of each for each loop. I use this so that when I process the submit I can for loop through the provide array and pull the corresponding values from the price array and process each set.

Tried 1.5.3, 1.6.0RC1, and now trunk.

Thanks,

MH

Comments

This sort of thing is simply not possible with Zend_Form's design; elements need to have unique names for purposes of overloading and element retrieval from the form. Based on your structure, I'm inclined to suggest utilizing subforms to aggregate these elements, and then after processing the form aggregating the individual variables in each.

Regardless, there are no changes we can make to accomodate this at this time.