简体   繁体   中英

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?

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();

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