这是我在我的控制器上的操作
public function invite()
{
if($this->getRequest()->isPost()){
$data_array = $this->getAllParams();
$emailList = explode(",", $data_array['emails']);
$message = $data_array['message'];
$error_emails = array();
if(sizeof($emailList) <= 1){
$this->view->message = '<div class="alert alert-danger"> Please enter more than one email address and please separate emails by a "," (comma)</div>';
}
else{
$x = 0;
foreach($emailList as $singleEmail){
if (!filter_var(trim($singleEmail), FILTER_VALIDATE_EMAIL)) {
$error_emails[] = $singleEmail;
}
else{
//send emails here
$x++;
}
}
if(sizeof($error_emails) > 0){
$email_str = implode(",",$error_emails);
if($x > 0 ){
$this->view->message = '<div class="alert alert-success"> Successfully Sent! </div> ';
}
$this->view->message .= '<br /><div class="alert alert-danger"> Message to this emails were not sent! <b>'.$email_str.'</b> <br /> Please enter valid emails!</div>';
}else{
$this->view->message = '<div class="alert alert-success"> Successfully Sent! </div>';
}
}
$request = $this->getRequest();
$request->setParam('emails', null);
$request->setParam('message', null);
}
}
我尝试了我发现的解决方案; 如您所见,我尝试使用setParam方法将值设置为null。不幸的是,这不起作用。 也没有设置阵列没有工作 - 不确定我是否做得对。 无论如何,我不想重定向。只是想取消设置。有人可以帮我吗?我现在已经尝试了几个小时。提前致谢。
答案 0 :(得分:2)
此问题并非特定于Zend Framework,有一种称为PRG(Post Redirect Get的缩写)的模式,用于处理表单的重新提交:https://en.wikipedia.org/wiki/Post/Redirect/Get
Zend Framework提供PRG控制器插件http://framework.zend.com/manual/current/en/modules/zend.mvc.plugins.html#post-redirect-get-plugin简而言之,它将会话数据存储在会话中并发布重定向,然后在后续获取请求中返回存储的帖子数据。
示例代码建议您在PRG插件重定向后在GET请求上处理表单:
// Pass in the route/url you want to redirect to after the POST
$prg = $this->prg('/user/register', true);
if ($prg instanceof \Zend\Http\PhpEnvironment\Response) {
// returned a response to redirect us
return $prg;
} elseif ($prg === false) {
// this wasn't a POST request, but there were no params in the flash messenger
// probably this is the first time the form was loaded
return array('form' => $form);
}
// $prg is an array containing the POST params from the previous request
$form->setData($prg);
if($form->isValid()) {
...
但我不同意这种方法,并建议始终在POST上处理表单并使用PRG显示表格,其中包含填充数据和验证消息(注意$form
这里是Zend\Form
的实例):< / p>
if($this->getRequest()->isPost()) {
$form->setData($this->getRequest()->fromPost());
if ($form->isValid()) {
//handle form
// if you don't need to show form again, redirect:
return $this->redirect()->toRoute('some/route');
}
}
// And now do PRG to re-display form with data and validation errors
$prg = $this->prg('/user/register', true);
if ($prg instanceof \Zend\Http\PhpEnvironment\Response) {
// returned a response to redirect us
return $prg;
} elseif ($prg === false) {
// this wasn't a POST request, but there were no params in the flash messenger
// probably this is the first time the form was loaded
return array('form' => $form);
}
// $prg is an array containing the POST params from the previous request
$form->setData($prg);
// make sure form have validation result to display
$form->isValid();
return array('form' => $form);
但由于您没有使用表单,因此需要手动验证数据两次。一旦处理并且一次显示错误消息。如果您不想使用Zend \ Form组件,我建议您查看Zend \ InputFilter以验证输入。
答案 1 :(得分:0)
您可以在zend中使用CSRF保护,即在提交表单时使用令牌