Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 Render Form in Controller

Tags:

forms

symfony

How do I render a form in a controller (not in a template)? I'm looking for something like:

$form = $this->createForm(...
$output = $form->render();

$output would be the html for the form.

like image 744
orourkedd Avatar asked Nov 01 '12 22:11

orourkedd


1 Answers

The form is merely an object, it doesn't know what it's layout is supposed to be --- that's what the template is for. If you're in a controller extending from the default controller, you can get the HTML of a rendered template like so: $html = $this->renderView('YourAppBundle:Blah:form.html.twig', array('form' => $form->createView() ) ); where that template contains the form markup/rendering code.

like image 116
Lusitanian Avatar answered Sep 24 '22 05:09

Lusitanian