Up to Symfony 4, I used to load a form, via Ajax, returning something like this:
$result = $this->renderView('my/template.html.twig',[
'form' => $form->createView()
]);
return new Response($result);
Since Symfony 5, this is not working anymore. I tried every posible combination using $this->renderForm()
, $this->render()
and 'form'=>$form
, with no success. What's the trick?
Try with
return new JsonResponse(['view' => $this->render('path/to_view.html.twig')->getContent()]);
It will return a json response to your Ajax call. You can access to 'view' key (name it as you want) which contain the html content of the page rendering [$this->render()->getContent
]. This is the way I use in my Symfony 5.4 project.