Search code examples
phpzend-frameworkzend-formsubformzend-form-sub-form

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


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]?


Solution

  • 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);
    }