【问题标题】:ZF2 - Separating one form in many tabsZF2 - 在多个选项卡中分离一个表单
【发布时间】:2014-01-31 16:41:45
【问题描述】:

我需要帮助.. 我有一个包含多个字段集的独特表单,我需要在选项卡中分隔一些字段集..

所以,我在视图中尝试了(表单是整个表单的变量):

$form = $this->form;
$customFieldset = $form->get('customFieldset');
$form->remove('customFieldset');

它可以工作,我的字段集表单在 $customFieldset.. 但是,我无法呈现这个! 尝试时:

echo $this->form($customFieldset);
//OR
echo $this->formInput($customFieldset);
//OR
$this->formCollection($customFieldset);

这些都不起作用..

我做得对吗?我该怎么办?

非常感谢。

【问题讨论】:

    标签: php forms tabs zend-framework2 fieldset


    【解决方案1】:

    为了达到您想要的结果(在多个选项卡中使用表单,最好根据选项卡的编号以不同的方式构造表单。例如,您的表单构造方法如下所示:

    <?php
    namespace Application\Form;
    
    use Zend\Form\Form;
    
    // A form model
    class YourForm extends Form
    {
      // Constructor.   
      public function __construct($tabNum)
      {
        // Define form name
        parent::__construct('contact-form');
    
        // Set POST method for this form
        $this->setAttribute('method', 'post');
    
        // Create the form fields here ...  
        if($tabNum==1) {
           // Add fields for the first tab
        } else if($tabNum==2) {
           // Add fields for the second tab
        } 
      }
    }
    

    在上面的示例中,您将$tabNum 参数传递给表单模型的构造函数,构造函数方法会根据其值创建一组不同的字段。

    在控制器的操作中,您使用如下表单模型:

    <?php
    namespace Application\Controller;
    
    use Application\Form\ContactForm;
    // ...
    
    class IndexController extends AbstractActionController {
    
      // This action displays the form
      public function someAction() {
    
        // Get tab number from POST
        $tabNum = $this->params()->fromPost('tab_num', 1);       
    
        // Create the form
        $form = new YourForm($tabNum);
    
        // Check if user has submitted the form
        if($this->getRequest()->isPost()) {
    
          // Fill in the form with POST data
          $data = $this->params()->fromPost();            
          $form->setData($data);
    
          // Validate form
          if($form->isValid()) {
    
            // Get filtered and validated data
            $data = $form->getData();
    
            // ... Do something with the validated data ...
    
            // If all tabs were shown, redirect the user to Thank You page
            if($tabNum==2) {
              // Redirect to "Thank You" page
              return $this->redirect()->toRoute('application/default', 
                array('controller'=>'index', 'action'=>'thankYou'));
            }
          }            
        } 
    
        // Pass form variable to view
        return new ViewModel(array(
              'form' => $form,
              'tabNum' => $tabNum
           ));
      }
    }
    

    在您的视图模板中,您使用以下代码:

    <form action="">
      <hidden name="tab_num" value="<?php echo $this->tabNum++; ?>" />
      <!-- add other form fields here -->
    </form>
    

    【讨论】:

    • 无论如何我都需要在视图中创建选项卡,对吧?
    • 我对 ZF 不是很了解,我不确定你说的是否明白。我的表单上的不同文件中有几个 FIELDSETS。我也使用教义。如果我没记错的话,使用 FIELDSET 就像几种形式,我可以为任何形式调用它,对吧?你能给我解释一下吗?
    • 我刚刚展示了如何创建一个可以跨多个选项卡使用的表单模型类。当然,您需要创建一个.phtml 视图模板并在那里为您的选项卡添加 HTML 代码。如果您对此有疑问,我会向您推荐 Using Zend Framework 2 的书,它非常适合初学者。
    • 谢谢.. 但显然就像我所做的那样,我只是不发送 tabNum.. 我创建了我的字段集,将所有内容放在同一个类“YourForm”中,并将它们分开查看..我是对的吗?我怎么能把它们分开你说的?我会买这本电子书,但是,如果你能回答我,我会更感激的! :P
    • 我已经扩展了答案。希望这篇文章对您有所帮助。
    猜你喜欢
    • 2015-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-31
    • 1970-01-01
    • 1970-01-01
    • 2017-07-13
    相关资源
    最近更新 更多