samedi 28 janvier 2017

Submit form to another controller in Symfony

I have a page index.html.twig which contains a Form this form when submitted, get validated and the result is shown in a page success.html.twig. Now I have a new requirement where the page success.html.twig itself contains a Form which should contain the values which were passed by the form from index.html.twig and if the user wants the new form should also allow the user to do search directly from success.twig.html. The requirement is inspired by hostel world.

Questions:

  1. Is there a design pattern which I could use to implement a solution
  2. My current thinking is to create a new Action for success.html.twig and submit the form to thatAction instead of rendering success.html.twig from index.html.twig's Action. Is it correct? How can I implement it?

Code (Partial):

 /**
 * @Route("/", name="homepage")
 */
public function indexAction(Request $request)
{
    $event = new Event();
    $form = $this->createForm(MyForm::class, $event);
    $form->handleRequest($request);

    if($form->isSubmitted() && $form->isValid()) {
        $event->setPlace($form["place"]->getData());


        $event->setDate($form["date"]->getData()->modify('+12 hours'));
        return $this->render('default/frontend/success.html.twig',
            array('events' => $events, 'cityName' => $cityName, 'cityImage' => $cityImage)
        );
    }

    return $this->render('default/frontend/index.html.twig', array('form' => $form->createView()));
}

Aucun commentaire:

Enregistrer un commentaire