【问题标题】:Prevent multiple form submission using PRG?防止使用 PRG 提交多个表单?
【发布时间】:2018-01-03 03:55:22
【问题描述】:

我需要去掉“确认重新提交”对话框以防止多次提交表单,并且还需要在用户刷新页面时清除 ZF2 自动呈现的表单验证错误。

我已经阅读了 ZF2 关于 PRG 插件的文档,但是当我仍然想显示表单错误时,我不确定如何实现它。

这是我当前的代码:

public function loginAction()
{
    $sm                 = $this->getServiceLocator();
    $forms              = $this->getForms();
    $viewModel          = new ViewModel();
    $viewModel->setTemplate('customer/customer/view-login-reg-form.phtml');
    $this->layout()->setVariable('title', 'Welcome!');
    $viewModel->setVariables($forms);

    $request            = $this->getRequest();
    if($request->isPost()) {
        $postData       = $request->getPost();
        $forms['formLogin']->setData($postData);
        $forms['formLogin']->setInputFilter($sm->get('Customer\Form\Filter\LoginFilter')->getInputFilter());

        if ($forms['formLogin']->isValid()) {
            $data       = $forms['formLogin']->getData();
            $customer   = $this->getCustomerTable()->getCustomer($data['login-email'], $data['login-password']);

            if (empty($customer)) {
                $viewModel->setVariable('errorMessage', 'Account does not exist');

                return $viewModel;
            }

            $LoginService = $sm->get('Customer\Service\LoginService');
            $LoginService->initLogin($customer);

            $this->handleRedirect();
        }
    }

    return $viewModel;
}

【问题讨论】:

  • 为什么会出现表单错误时多次提交的问题?如果有错误,是否重新提交表单都没有关系。您的代码看起来不错。
  • @TimFountain 他们希望我摆脱 所有 表单的“确认重新提交”提示(要统一?)以避免双重处理,例如双重插入记录。

标签: zend-framework2 post-redirect-get


【解决方案1】:

您可以编写如下方法:

public function loginAction()
{
    $prg = $this->prg();
    if ($prg instanceof Response) {
        return $prg;
    }
    // here, $prg is false if multiple submissions 
    if ($prg === false) {
        // your code for init $prg or redirect to other action
    }
    // here, $prg contains post and get parameters as an array
    // You must distinguish whether this is an entry to display the form 
    // or the return to process the data returned by the submit
    // because $request is no longer available. Example :
    $forms              = $this->getForms();
    if (array_key_exists('submit', $prg) {
        $forms['formLogin']->setData($prg);
        $forms['formLogin']->setInputFilter($sm->get('Customer\Form\Filter\LoginFilter')->getInputFilter());

        if ($forms['formLogin']->isValid()) {
            $data       = $forms['formLogin']->getData();
            $customer   = $this->getCustomerTable()->getCustomer($data['login-email'], $data['login-password']);

            if (empty($customer)) {
                $viewModel->setVariable('errorMessage', 'Account does not exist');

                return $viewModel;
            }

            $LoginService = $sm->get('Customer\Service\LoginService');
            $LoginService->initLogin($customer);

            $this->handleRedirect();
        }
    }
    $viewModel          = new ViewModel();
    $viewModel->setTemplate('customer/customer/view-login-reg-form.phtml');
    $this->layout()->setVariable('title', 'Welcome!');
    $viewModel->setVariables($forms);
    return $viewModel;
}

【讨论】:

  • 我对其进行了一些修改,但它确实有效。并感谢 cmets。 :)
  • 等一下,我有一个问题,这仅适用于自我重定向吗?如果我需要的表单处理在另一条路线上怎么办?是否可以将 POST 数据传递到该路由? (并在处理后将用户重定向到另一个页面)
  • 您可以通过$this->prg($route, false) 重定向到另一个路由或通过$this->prg($url, true) 重定向到另一个url。在目标操作中,$this->prg() 获取参数(POST 和 GET)。处理后,将用户重定向到另一个路由,return $this->redirect()->toRoute($route, ['action' => $action]);
  • 谢谢!但是,如果我的理解是正确的:假设我重定向到另一个路由来处理表单,并且在表单验证中发现了错误,那么当我重定向回来时,我必须携带这些错误(可能存储在会话中)以显示它们到上一条路线(返回表格)?
猜你喜欢
  • 1970-01-01
  • 2014-09-04
  • 1970-01-01
  • 1970-01-01
  • 2013-03-18
  • 1970-01-01
  • 1970-01-01
  • 2017-10-02
相关资源
最近更新 更多