如何将两个Zend_Forms组合成一个Zend_Form?

时间:2010-02-03 18:22:16

标签: zend-framework zend-form

我有两个Zend_Forms(form1和form2)。我想将它们组合在一起,所以我有第三种形式(form3),由两种形式的所有元素组成。

使用Zend Framework执行此操作的正确方法是什么?

2 个答案:

答案 0 :(得分:3)

这是我最终如何做到的...我不想命名每个表单,我只是想要表单中的所有元素,所以我决定单独添加所有元素而不是使用子表单。

<?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());
    }
}

答案 1 :(得分:1)

您可以使用子表单。 Zend_FormZend_Form_SubForm之间的唯一区别是装饰器:

$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);

然后在控制器中将表单分配给视图:

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

您可以按名称访问视图中的两个子表单:

// In the view
echo $this->form->form_1;
echo $this->form->form_2;
相关问题