如何在Zend框架中刷新页面时防止表单提交


How to prevent form submission on refreshing the page in Zend framework?

这是我在控制器上的操作

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。不幸的是,这不起作用。未设置的数组也不起作用 - 不确定我是否做对了。无论如何,我不想重定向。只想取消设置。有人可以帮助我吗?我已经尝试了几个小时了。提前谢谢。

这个问题不是特定于 Zend 框架的,有一种称为 PRG(Post Redirect Get 的缩写)的模式来处理表单的重新提交: https://en.wikipedia.org/wiki/Post/Redirect/Get

Zend 框架提供了 PRG 控制器插件 http://framework.zend.com/manual/current/en/modules/zend.mvc.plugins.html#post-redirect-get-plugin 简而言之,它将帖子数据存储在会话中并发出重定向,然后在随后的 get 请求中返回存储的帖子数据。

示例代码建议您在 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 的一个实例):

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来验证输入。

您可以在 zend 中使用 CSRF 保护,即在提交表单时使用令牌