【问题标题】:How to combine two Zend_Forms into one Zend_Form?如何将两个 Zend_Form 组合成一个 Zend_Form?
【发布时间】:2011-01-12 18:03:45
【问题描述】:

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

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

【问题讨论】:

    标签: zend-framework zend-form


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      您可以使用子表单。 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;
      

      【讨论】:

      • 在我看来,我将如何访问表单元素? $this->form->...?
      猜你喜欢
      • 2011-09-27
      • 2015-09-26
      • 2011-07-31
      • 2010-10-16
      • 1970-01-01
      • 2011-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多