Zend Framework 1.7。使用一个安静的控制器。已实施newAction
和postAction
。
在postAction
数据验证失败时,我想重定向回来,我想查看填充了params(以及错误消息)的表单。
//controller
public function newAction() {
$form = new My_Form_form ();
$this->view->form = $form;
}
public function postAction() {
$newData = $this->getRequest ()->getPost ();
$filters = array();
$validators = array (
'name' => array (
'presence' => 'required'
)
);
$input = new Zend_Filter_Input($filters, $validators);
$input->setData($newData);
if (!$input->isValid()) {
$this->_helper->redirector ( 'new', 'controller' );
}
}
将表单重定向提交至PostAction
。重定向不会保存当前的参数,因此表单不会填充先前的数据。
进行正确重定向的更好方法是什么?
答案 0 :(得分:0)
你必须在newAction中移动帖子,如下所示:
//controller
public function newAction() {
$form = new My_Form_form ();
$newData = $this->getRequest()->getPost();
if(!empty($newData){
$filters = array();
$validators = array (
'name' => array (
'presence' => 'required'
)
);
$input = new Zend_Filter_Input($filters, $validators);
$input->setData($newData);
}
$this->view->form = $form;
}
答案 1 :(得分:0)
为什么不在Form Class中设置Filters和Validators并使用两个Actions?
请参阅:http://framework.zend.com/manual/1.12/en/zend.form.quickstart.html
此代码可以为您提供帮助。
public function newAction()
{
//init the form
$this->view->form = $form = new Your_Form();
if($this->_request->isPost()) {
$formData = $this->_request->getPost();
if($form->isValid($formData)) {
//get form data with filters
$data = $form->getValues();
//do something
//redirect
$this->_helper->redirector("index");
} else {
/* Form Error */
$form->populate($formData);
}
}
}