ZF: ZEND_SUBFORM - how to make element name like list[id][name]?

383 views Asked by At

I create elements for subform:

for($i = 1; $i<10; $i++)
     $name = new Zend_Form_Element_Text("[$i][name]");

But [ and ] will be deleted after dispatch page.

So how to setup name like list[id][name]?

2

There are 2 answers

1
squirrel On BEST ANSWER

Create another Zend_Form_SubForm for each $i:

for ($i = 1; $i < 10; $i++) {
    $subform = new Zend_Form_SubForm();
    $subform->addElement('text', 'name');
    $mainform->addSubForm($subform, $i);
}

Text elements will be named "1[name]", "2[name]", and so on. If you want them to be named "list[1][name]" then you need another level of subform:

$listform = new Zend_Form_SubForm();
$mainform->addSubForm($listform, 'list');
for ($i = 1; $i < 10; $i++) {
    $listsubform = new Zend_Form_SubForm();
    $listsubform->addElement('text', 'name');
    $listform->addSubForm($listsubform, $i);
}
4
Arek Jablonski On

Use

$element->setBelongsTo( $array )

and in that $array you can define as many levels of [] as you need, you don't have to create for that many levels of subForms. You don't have to create for that even any subForms.

Enjoy!