Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

symfony3 embedded controller with form

I have an embedded controller as described at Symfony's website. http://symfony.com/doc/current/templating/embedding_controllers.html

The only difference is that my controller has a form. All is rendered correctly but once the form is submitted, the request is always empty. Since the request is empty, the $form->isValid() and $form->isSubmitted() always return false.

Please find below my code:

Twig:

{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}

Controller:

public function myEmbeddedAction(Request $request)
    {
      $template ="myTwig.html.twig";
      $supportTicket = new SupportTicket();
      $form = $this->createForm('AppBundle\Form\SupportTicketType', $supportTicket);
      $form->handleRequest($request);

      if ($form->isSubmitted() && $form->isValid()) {
        // success
      }
      return $this->render($template, array(
          'supportTicket' => $supportTicket,
          'form' => $form->createView()

      ));
}

Embedding the controller in my layout:

{{ render(controller('AppBundle:Default:myEmbedded')) }}

The request at the embedded action has always the same value:

{"attributes":{},"request":{},"query":{},"server":{},"files":{},"cookies":{},"headers":{}}

However, if I access the embedded controller directly through the URL and fill the form, it works. How can I get the form to work inside of my embedded controller?

like image 935
Sami Avatar asked Sep 03 '25 09:09

Sami


1 Answers

Just for grins try

$request = $this->get('request_stack')->getMasterRequest(); 

When using embedded controllers you actually receive a sub request. I would have thought that the sub request will still have the master request attributes but I guess not.

Update: 2019-09-16

While the above code will still work when extending from AbstractController, it would be more in keeping with the spirit of Symfony to use injection:

public function myEmbeddedAction(RequestStack $requestStack)
{
    $request = $requestStack->getMasterRequest();
like image 135
Cerad Avatar answered Sep 04 '25 23:09

Cerad