zf2 - 需要一页多页表单的帮助

zf2 - need help on multi page form in one page

刚学zf2,官方文档的基础教程很好。现在,我想挑战自己,在一页中创建多页表单,类似的东西 http://demo.stepblogging.com/multi-step-form/

因此,目前我有两种形式,分别称为 "Contact Form" 和 "Album Form"。

我的想法是拆分为 2 种形式。问题是当我完成第一个表格的所有字段时,我不确定如何转到下一个表格。我不确定我是否可以在 Controller 中执行逻辑,我知道大多数在线教程都使用 javascript 来处理下一步和后退按钮。或者有更好的主意?

这是我的控制器页面。

 public function multipleAction(){
    $formOne = new ContactForm();
    $formTwo = new AlbumForm();

    $formOne->get('next')->setValue('Next');
    $request = $this->getRequest();

    if($request->isPost()){ 
        $aa = new ContactFilter();
        $formOne->setInputFilter($aa); 
        $formOne->setData($request->getPost()); 

        if ($formOne->isValid()) {
           //save session 
           //maybe display second form or any other solution
        }
    }

我的 multiple.phtml 页面包含 2 个表单

<ul id="signup-step">
<li id="contact" class="active">Contact</li>
<li id="album">Album</li>
</ul>

<?php
 $form_one = $this->form_one;

 //$form_one->setAttribute('action', $this->url('album', array('action' => 'multiple')));
 $form_one->prepare();

 //echo $_SESSION['name'];
 echo $this->form()->openTag($form_one); ?>

 <div id="contact-field">
<legend>Contact</legend>
<?php
 echo $this->formHidden($form_one->get('id'));

 echo $this->formLabel($form_one->get('name')).'<br>';
 echo $this->formInput($form_one->get('name'))."<br>";
 echo $this->formElementErrors($form_one->get('name'));

 echo $this->formLabel($form_one->get('artist')).'<br>';
 echo $this->formInput($form_one->get('artist'))."<br>";
 echo $this->formElementErrors($form_one->get('artist'));


 echo $this->formLabel($form_one->get('address')).'<br>';
 echo $this->formInput($form_one->get('address'))."<br><br>";
 echo $this->formElementErrors($form_one->get('address'));

 echo $this->formSubmit($form_one->get('next'));
 echo $this->form()->closeTag($form_one);
 ?>

 <?php
 $form_two = $this->form_two;
 $form_two->prepare();

 echo $this->form()->openTag($form_two); ?>

 <div id="album-field" >
<legend>Album</legend>
<?php 
 echo $this->formLabel($form_two->get('title')).'<br>';
 echo $this->formInput($form_two->get('title'))."<br>";
 echo $this->formElementErrors($form_two->get('title'));

 echo $this->formLabel($form_two->get('artist'))."<br>";
 echo $this->formInput($form_two->get('artist'))."<br>";
 echo $this->formElementErrors($form_two->get('artist'));
 echo $this->form()->closeTag($form_two);
 ?>

我不认为我已经完全理解你想要什么, 但如果你指的是不同形式的多个步骤,那么你只需要把 会话中的前一步结果,

你的问题很模糊。您给定的 link 也是由 jquery 完成的!...据我了解,您希望在不使用 js/jquery.

的情况下使用此演示页面行为
1. break this one form into three form. For easiness add a hidden field step
2. make next and back button input type submit
3. after submit in your action check submitted value and determine what you want ...[show next form or whatever]

下面的代码非常粗糙,仅用于示例目的(在 10 分钟内完成并看起来像)

所以基本上我们所做的就是以下 -

  1. 创建会话
  2. 检查会话中是否有该特定表单的任何数据,如果有则使用 setData() 在表单上设置数据
  3. 如果表单发布数据,则对其进行验证和过滤,然后将该数据保存到会话中
  4. 表单中的 "Submit" 按钮值用于确定导航路径,即上一页或下一页。
  5. 最后一页会将会话中的数据保存到数据库(或任何您想用它做的事情),当然也会销毁会话。

控制器

/** @var Zend\Session\Container Session Object */
private $session;

public function __construct()
{
    // Put a meaningful name in the constructor
    $this->session = new Container();
}

public function page1Action()
{
    // This should really be injected...
    $form = new Form\Form1();

    // Proper validation & filtering needs to be done for every form (this is example only)
    $form->setInputFilter(new InputFilter());

    // Populates form data from the session if it already exists
    if (isset($this->session->form1Inputs)) {
        $form->setData($this->session->form1Inputs);
    }

    $request = $this->getRequest();

    if ($request->isPost()) {
        $form->setData($request->getPost());

        if ($form->isValid()) {
            $formData = $form->getData();

            // Saves the new data to the session
            $this->session->form1Inputs = $formData;

            // Redirects to next page
            if ($formData['Next'] === 'Next') {
                $this->redirect()->toRoute('application', ['controller' => 'Index', 'action' => 'page2']);
            }
        }
    }

    return new ViewModel(['form' => $form]);
}

public function page2Action()
{
    // This should really be injected...
    $form = new Form\Form2();

    // Proper validation & filtering needs to be done for every form (this is example only)
    $form->setInputFilter(new InputFilter());

    // Populates form data from the session if it already exists
    if (isset($this->session->form2Inputs)) {
        $form->setData($this->session->form2Inputs);
    }

    $request = $this->getRequest();

    if ($request->isPost()) {
        $form->setData($request->getPost());

        if ($form->isValid()) {
            $formData = $form->getData();

            // Saves the new data to the session
            $this->session->form2Inputs = $formData;

            // Redirects to next or previous page
            if ($formData['Next'] === 'Next') {
                $this->redirect()->toRoute('application', ['controller' => 'Index', 'action' => 'page3']);
            } elseif ($formData['Previous'] === 'Previous') {
                $this->redirect()->toRoute('application', ['controller' => 'Index', 'action' => 'page1']);
            }
        }
    }

    return new ViewModel(['form' => $form]);
}

public function page3Action()
{
    // This should really be injected...
    $form = new Form\Form3();

    // Proper validation & filtering needs to be done for every form (this is example only)
    $form->setInputFilter(new InputFilter());

    // Populates form data from the session if it already exists
    if (isset($this->session->form2Inputs)) {
        $form->setData($this->session->form2Inputs);
    }

    $request = $this->getRequest();

    if ($request->isPost()) {
        $form->setData($request->getPost());

        if ($form->isValid()) {
            $formData = $form->getData();

            // Saves the new data to the session
            $this->session->form3Inputs = $formData;

            // Finalise or redirect to previous page
            if ($formData['Finalise'] === 'Finalise') {
                // Save all data to DB from session & destroy
            } elseif ($formData['Previous'] === 'Previous') {
                $this->redirect()->toRoute('application', ['controller' => 'Index', 'action' => 'page2']);
            }
        }
    }

    return new ViewModel(['form' => $form]);
}

表格 1

public function __construct($name = null)
{
    parent::__construct($name);

    $this->add(['name' => 'Form1Text1', 'type' => 'Text', 'options' => ['label' => 'Form 1 Text 1 : ']]);

    $this->add(['name' => 'Form1Text2', 'type' => 'Text', 'options' => ['label' => 'Form 1 Text 2 : ']]);

    $this->add(['name' => 'Next', 'type' => 'Submit', 'attributes' => ['value' => 'Next']]);
}

表格 2

public function __construct($name = null)
{
    parent::__construct($name);

    $this->add(['name' => 'Form2Text1', 'type' => 'Text', 'options' => ['label' => 'Form 2 Text 1 : ']]);

    $this->add(['name' => 'Form2Text2', 'type' => 'Text', 'options' => ['label' => 'Form 2 Text 2 : ']]);

    $this->add(['name' => 'Next', 'type' => 'Submit', 'attributes' => ['value' => 'Next']]);
    $this->add(['name' => 'Previous', 'type' => 'Submit', 'attributes' => ['value' => 'Previous']]);
}

表格 3

public function __construct($name = null)
{
    parent::__construct($name);

    $this->add(['name' => 'Form3Text1', 'type' => 'Text', 'options' => ['label' => 'Form 3 Text 1 : ']]);

    $this->add(['name' => 'Form3Text2', 'type' => 'Text', 'options' => ['label' => 'Form 3 Text 2 : ']]);

    $this->add(['name' => 'Previous', 'type' => 'Submit', 'attributes' => ['value' => 'Previous']]);
    $this->add(['name' => 'Finalise', 'type' => 'Submit', 'attributes' => ['value' => 'Finalise']]);
}