我刚刚学习zf2,官方文档的基础教程非常棒。现在,我想挑战自己在一个页面中创建多页面表单,类似于http://demo.stepblogging.com/multi-step-form/
所以,目前我有两种名为" Contact Form"和#34;专辑形式"。
这个想法是我想分成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);
?>
答案 0 :(得分:0)
我不认为我完全理解你想要的东西, 但如果你的意思是不同形式的多个步骤,所以你只需要把它 上一步结果在会话中,
答案 1 :(得分:0)
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]
答案 2 :(得分:0)
以下代码非常粗糙,仅用于示例目的(在10分钟内完成并且看起来像这样)
所以基本上我们所做的就是以下内容 -
控制器的
/** @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']]);
}