开发者

How to combine two Zend_Forms into one Zend_Form?

I have two Zend_Forms (f开发者_如何学JAVAorm1 and form2). I would like to combine them so I have a third form (form3) consisting of all the elements from both forms.

What is the correct way to do this with the Zend Framework?


Here's how I ended up doing it...I didn't want to namespace each form, I just wanted all the elements in the form so I decided to just add all the elements individually instead of using subForms.

<?php

class Form_DuplicateUser extends Zend_Form
{
    public function init()
    {
        $this->setMethod('post');

        $form1 = new Form_ContactPrimaryInformationForm();
        $this->addElements($form1->getElements());

        $form2 = new Form_ContactAdditionalInformationForm();
        $this->addElements($form2->getElements());
    }
}


You can use subforms. The only difference between Zend_Form and Zend_Form_SubForm are the decorators:

$form1 = new Zend_Form();
// ... add elements to $form1
$form2 = new Zend_Form();
// ... add elements to $form2

/* Tricky part:
 * Have a look at Zend_Form_SubForm and see what decorators it uses.
 */
$form1->setDecorators(array(/* the decorators you've seen */));
$form2->setDecorators(array(/* ... */));

$combinedForm = new Zend_Form();
$combinedForm->addSubForm('form_1', $form1);
$combinedForm->addSubForm('form_2', $form2);

Then in the controller you assign the form to the view:

$this->view->form = $combinedForm;

And you can acces the two subforms in the view by name:

// In the view
echo $this->form->form_1;
echo $this->form->form_2;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜