Reputation: 12142
I need to obtain something like this in zend
<input type="text" name="phone[1]" value="" />
<input type="text" name="address[1]" value="" />
<input type="text" name="banana[1]" value="whatever" />
Notice they have the same id inside the brackets! (i don't need name="phone[]"
, or name="phone[phone1]"
)
I've tried and https://stackoverflow.com/a/3673034/579646 and https://stackoverflow.com/a/406268/579646 and https://stackoverflow.com/a/7061713/579646
The problem is in ZendFramework i end up having to name 3 elements with the same name "1" and the last overwrites the previous. Even if i create 3 subforms i get the same effect.
Different examples show how to obtain an array with different indexes or no index([]
), but i need different array to have the same index.
Thanks
Upvotes: 2
Views: 623
Reputation: 6429
Zend_Form has a feature for this named setElementsBelongTo
. See
http://framework.zend.com/manual/1.12/en/zend.form.advanced.html
The way of use this is setting to the Zend_Form object the prefix with setElementsBelongTo
, if you want iterate over each field then you can use subforms to encapsulate each group of fields
You can call to setElementsBelongTo
in your controller or in the init()
method of your form class:
$mainForm = new Zend_Form();
$phoneForm = new Zend_Form_Subform();
$element = $phoneForm->createElement('text', '1'); // 1 is the element inside of the brackets
$phoneForm->addElement($element);
$phoneForm->setElementsBelongTo('phone'); // phone is the part leading the brackets
$mainForm->addSubform($phoneForm, 'phone_form');
$phoneForm = new Zend_Form_Subform();
$element = $phoneForm->createElement('text', '2'); // 1 is the element inside of the brackets
$phoneForm->addElement($element);
$phoneForm->setElementsBelongTo('phone'); // phone is the part leading the brackets
$mainForm->addSubform($phoneForm, 'phone_form2');
$addressForm = new Zend_Form_Subform();
$element = $addressForm->createElement('text', '1');
$addressForm->addElement($element);
$addressForm->setElementsBelongTo('address');
$mainForm->addSubform($addressForm, 'address_form');
echo $mainForm;
var_dump($mainForm->getValues());
gives
array(2) {
["phone"]=> array(2) { [1]=> NULL [2]=> NULL }
["address"]=> array(1) { [1]=> NULL } }
To get your expected result you will need remove some decorators (Form, dt, etc):
<input type="text" name="phone[1]" value="" />
<input type="text" name="address[2]" value="" />
Then when you retrieve the values with $form->getValues()
the result is:
Array(
'phone' = Array(
'1' => <value>,
),
'address' = Array(
'1' => <value>,
)
);
Upvotes: 4
Reputation: 4616
i do not understand, why you need this special case, but the only possible solution in my opinion is, to use a custom template.
class YourForm extends Zend_Form
{
public function init()
{
$this->setDecorators(array(
array(
'ViewScript',
array(
'viewScript' => 'path/to/your/phtml/file',
'possibleOtherParamYouWantToPass' => 'value',
...
)
)
));
}
}
So what you do is, you say that you want to use a template file, where you can declare everything by yourself. Also your banana[1]
.
But you will lose simple validation and other benefits.
Upvotes: 0