ZF2, preventing multiple form feed

Is there any way in ZF2 forms to prevent multiple forms from being submitted? I've tested elements Captcha

and CSRF

using a function isValid()

, but they don't prevent multiple views, especially those with a browser refresh button. thanks in advance

+3


source to share


1 answer


Yes there is a PRG controller plugin:

POST / REDIRECT / GET PLUGIN

To quote from the official zf2 docs:

When a user submits a POST request (for example, after submitting a form), their browser will try to protect them from POSTing again, hack the back button, causing browser warnings and pop-ups, and sometimes reformatting the form. Instead, when we receive a POST, we must store the data in the session container and redirect the user to a GET request.



http://framework.zend.com/manual/2.0/en/modules/zend.mvc.plugins.html#the-post-redirect-get-plugin

Expand further in your own words; when using this plugin, every time the form is POST, the POST variables are saved to SESSION and the user is redirected to either a different route or just the same route (update). The form variables are then accessed through the PRG plugin as a simple array that mimics the original POST array. This will prevent the FORM from being sent multiple times.

Usage (from ZF2 docs):

// 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' => $myForm);
}

// $prg is an array containing the POST params from the previous request
$form->setData($prg);

// ... your form processing code here

      

+5


source







All Articles