Nicolas de Fontenay
Nicolas de Fontenay

Reputation: 2200

Zend - Add subForm to existing display group

In my controller I got a function which looks like this:

public function newExperienceAction() {
        $this->_helper->layout->disableLayout();
        $ajaxContext = $this->_helper->getHelper('AjaxContext');
        $ajaxContext->addActionContext('newExperience', 'html')->initContext();

        $id = $this->_getParam('id', null);

        $this->form = new Application_Form_Cv();
        $this->experience = new Zend_Form_SubForm();
        $this->form->addSubForm($this->experience, 'experience');
        $rowExperience = new Application_Form_Experience();
        $rowExperience->setDisplayGroups('experience');
        $this->experience->addSubForm($rowExperience, "experience$id", $id+3);

        echo $rowExperience->__toString();
    }

When the user press (+) on the form, a new subForm will be displayed.

I'm currently in the process if shaping it into a table. I will have more than one subForm on this form so I need to use DisplayGroups.

In this situation, I believe I have to create a Display group when the form is first created.

Then I need to append the new subForms to the existing display groups.

So the question:

How do I add new subForms to an existing display group?

Upvotes: 1

Views: 3997

Answers (2)

Szymon Wygnański
Szymon Wygnański

Reputation: 11064

I'm afraid this is not possible now. I'm using Zend Framework version 1.11.7 and the code of the addDisplayGroup method looks like that:

foreach ($elements as $element) {
    if($element instanceof Zend_Form_Element) {
         (...)
    }

    if (isset($this->_elements[$element])) {
        (...)
    }
}

So this is not supported yet. You can add only Zend_Form_Element instances

Upvotes: 1

Simon
Simon

Reputation: 78

You can achieve this by simply adding

$rowExperience->setIsArray(true);

This causes the sub-form to act like a display group, i.e. its values are wrapped in an array indexed by the sub-form's name (experience$id in your case).

Upvotes: 1

Related Questions