Php ZF2-在多个选项卡中分离一个表单
我需要帮助。。 我有一个具有多个字段集的独特表单,我需要在选项卡中单独设置一些字段集 因此,我在视图中尝试(表单是我的变量,包含整个表单): 它可以工作,我的字段集表单是$customFieldset。。但是,我不能渲染这个! 尝试时:Php ZF2-在多个选项卡中分离一个表单,php,forms,tabs,zend-framework2,fieldset,Php,Forms,Tabs,Zend Framework2,Fieldset,我需要帮助。。 我有一个具有多个字段集的独特表单,我需要在选项卡中单独设置一些字段集 因此,我在视图中尝试(表单是我的变量,包含整个表单): 它可以工作,我的字段集表单是$customFieldset。。但是,我不能渲染这个! 尝试时: echo $this->form($customFieldset); //OR echo $this->formInput($customFieldset); //OR $this->formCollection($customFieldset
echo $this->form($customFieldset);
//OR
echo $this->formInput($customFieldset);
//OR
$this->formCollection($customFieldset);
这些都不管用
我做得对吗?我怎么能做到
非常感谢。为了获得您想要的结果(跨多个选项卡使用表单,最好根据选项卡的编号以不同的方式构造表单。例如,您的表单构造函数方法如下所示:
<?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
}
}
}
在视图模板中,使用以下代码:
<form action="">
<hidden name="tab_num" value="<?php echo $this->tabNum++; ?>" />
<!-- add other form fields here -->
</form>
无论如何,我需要在视图中创建选项卡,对吗?我对ZF不太了解,我不确定是否理解你所说的。我的表单上的不同文件中有几个字段集。我也使用原则。如果我没有弄错的话,使用字段集就像几个表单一样,我可以为任何表单调用它,对吗?你能解释一下吗?我刚刚演示了如何创建表单可以跨多个选项卡使用的模型类。当然,您需要创建一个.phtml
视图模板,并在那里为您的选项卡添加HTML代码。如果您对此有问题,我会向您推荐这本书,这本书非常适合初学者。谢谢。但显然与我所做的一样,我只是不发送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>