【发布时间】:2012-07-26 07:57:44
【问题描述】:
我在使用 Zend 表单验证失败后重新显示视图时遇到问题。
我的初始操作看起来像
public function test1Action() {
// get a form and pass it to the view
$this->view->form = $this->getForm();
// extra stuff I need to display
$this->view->name = "Bob";
}
还有风景
Hello <?= $this->name?>
<?php echo $this->form;?>
当表单提交后调用的动作“test2”出现验证错误时,问题就来了:
public function test2Action() {
if (!$this->getRequest()->isPost()) {
return $this->_forward('test1');
}
$form = $this->getForm();
if (!$form->isValid($_POST)) {
$this->view->form = $form;
return $this->render('test1'); // go back to test1
}
}
确实,变量“name”丢失了,视图不正确。而不是说“你好鲍勃”,而是说“你好”。
我应该如何处理?我应该重定向到 test1 而不是再次渲染 test1 吗?如何 ?
编辑
根据 Coder 先生的回答,我得到了以下结果:
控制器:
public function getForm() {
// what is important is that the form goes to action test3 and not test4
// SNIP form creation with a field username
return $form;
}
public function test3Action()
{
$this->view->form = $this->getForm();
$this->view->name = "Bob";
if(!$this->getRequest()->isPost())return;
if($this->view->form->isValid($_POST))
{
//save the data and redirect
$values = $this->view->form->getValues();
$username = $values["username"];
$defaultSession = new Zend_Session_Namespace('asdf');
$defaultSession->username = $username;
$this->_helper->redirector('test4');
}
}
public function test4Action()
{
$defaultSession = new Zend_Session_Namespace('asdf');
$this->view->username = $defaultSession->username;
}
test3.phtml
Hello <?= $this->name?>
<?php echo $this->form;?>
test4.phtml
success for <?php echo $this->username;?>
【问题讨论】:
标签: forms validation zend-framework