简体   繁体   中英

Symfony2 form submitting on page refresh

I have a form in Symfony2 framework. On successful submission of the page it renders another twig template file and returns the values by passing the parameters in an array. But after submission, if I refresh the page, again it is submitting the form and the table entry is created. Here is the code that is executed after submission in the controller,

$this->get('session')->setFlash('info', $this->get('translator')->trans('flash.marca'));

return $this->render('NewBundle:Backend:marca.html.twig', array(
                                        'active' => 1,
                                        'marca' => $marca,
                                        'data' => $dataCamp,
                                        'dataMarca' => $this->getMarcas($admin->getId()),
                                        'admin' => $admin,
            ));

I want the form to be redirected to the twig files mentioned there with the parameters and the alert message mentioned above. But I don't want the form to be submitted on page refresh.

Thanks

这对我有用:

return $this->redirectToRoute("route_name");

You should save submitted data in session and redirect user. Then you will be able to refresh page as much as you want without additional submission. Example code - your action algorithm should be similar:

...
/**
 * @Route("/add" , name="acme_app_entity_add")
 */
public function addAction()
{
    $entity = new Entity();
    $form = $this->createForm(new EntityType(), $entity);
    $session = $this->get('session');

// Check if data already was submitted and validated
if ($session->has('submittedData')) {
    $submittedData = $session->get('submittedData');
    // There you can remove saved data from session or not and leave it for addition request like save entity in DB
    // $session->remove('submittedData');

    // There your second template
    return $this->render('AcmeAppBundle:Entity:preview.html.twig', array(
        'submittedData' => $submittedData
        // other data which you need in this template
    ));
}

if ($request->isMethod('POST')) {
    $form->bindRequest($request);

    if ($form->isValid()) {
        $this->get('session')->setFlash('success', 'Provided data is valid.');
        // Data is valid so save it in session for another request
        $session->set('submittedData', $form->getData()); // in this point may be you need serialize saved data, depends of your requirements

        // Redirect user to this action again
        return $this->redirect($this->generateUrl('acme_app_entity_add'));
    } else {
        // provide form errors in session storage
        $this->get('session')->setFlash('error', $form->getErrorsAsString());
    }
}

return $this->render('AcmeAppBundle:Entity:add.html.twig', array(
    'form' => $form->createView()
));
}

Redirect to same page is preventing additional data submission. So lean of this example modify your action and you will be fine. Also instead save data in session you can pass it through redirect request. But I think this approach is more difficult.

  1. Save your data (session/db/wherever you want it saved)
  2. redirect to a new action, retreiving the new data in that action, and rendering the template

this way, refreshing the new action, will only refresh the template, as the saving of your data happened in the previous action

understand ?

so basically replace your

return $this->render....

by

return $this->redirect($this->generateUrl('ROUTE_TO_NEW_ACTION')));

and in this new action, you put your

return $this->render....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM