使用PRG阻止多个表单提交?

时间:2018-01-03 03:55:23

标签: zend-framework2 post-redirect-get

我需要摆脱'确认重新提交'用于防止多个表单提交的对话框,以及用户刷新页面时ZF2自动呈现的表单验证错误。

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

这是我目前的代码:

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;
}

1 个答案:

答案 0 :(得分:0)

您可以按照以下方式编写方法:

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;
}