【问题标题】:How to prevent form submission on refreshing the page in Zend framework?如何防止在 Zend 框架中刷新页面时提交表单?
【发布时间】:2023-03-03 06:59:25
【问题描述】:

这是我对控制器的操作

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

【问题讨论】:

  • 尝试标头php.net/manual/en/function.header.php 并强制重新验证。会话/令牌也是另一种选择。
  • 您避免重定向的原因是什么?
  • 为什么使用html的字符串而不是控制器插件FlashMessenger?

标签: php post zend-framework2 page-refresh unset


【解决方案1】:

这个问题不是 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 简而言之,它将 post 数据存储在 session 中并发出重定向,然后在后续的 get 请求中返回存储的 post 数据。

示例代码建议您在 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 来验证输入。

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多