0

how can I prevent multiple form submit? Every time some one sending me form like 2-Xx in a row. It looks like they're spamming "enter" button on keyboard.

Tahanks

1 Answers1

1

You should redirect the user after the form has been submitted to prevent the user's browser from re-sending the POST request if Enter is pressed or the page is being refreshed.

Just send a HTTP 302 (temporary) redirect if the form is valid like this in your controller:

if ($form->isValid()) {
  $data = $form->getData();
  $em->persist($data);
  $em->flush();

  return $this->redirectToRoute('route_submit_success');            
}
Nicolai Fröhlich
  • 51,330
  • 11
  • 126
  • 130
  • Some client side code would be need too in case user is pressing submit button multiple times, if that is the case submit button could be disabled on click considering client side validation of course! check [this question](https://stackoverflow.com/questions/2830542/prevent-double-submission-of-forms-in-jquery) for reference! – Muzafar Ali May 31 '17 at 09:35
  • Please share more details. How does this ensure that the user does not click the submit button multiple times and multiple entities are persisted? We are also using a redirect, as you suggest, but it does not avoid that `persist` is called multiple times – Nico Haase Nov 09 '22 at 11:11