Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set the url for the form to POST using the FormBuilder in Symfony2?

Tags:

I'm dynamically loading different form classes in my Controller and displaying them in my template. This works fine, except that the Symfony2 docs show adding the route for the form to POST to in the template by hand.

<form action="{{ path('task_new') }}" method="post" {{ form_enctype(form) }}>     {{ form_widget(form) }}      <input type="submit" /> </form> 

I need to set that form action in the FormBuilder class-- the POST routes (e.g. 'task_new') are different depending on the form class I'm using. Is there a way to set the form action url in the FormBuilder class? How can we get {{ form_widget(form) }} to render the complete form, and not just the rows? Thanks!

like image 627
Acyra Avatar asked Mar 18 '12 11:03

Acyra


1 Answers

It is possible out of the box -- http://symfony.com/doc/current/book/forms.html#changing-the-action-and-method-of-a-form

$form = $this->createFormBuilder($task) ->setAction($this->generateUrl('target_route')) ->setMethod('GET') ->add('task', 'text') ->add('dueDate', 'date') ->add('save', 'submit') ->getForm(); 
like image 80
Pratyush Avatar answered Sep 21 '22 06:09

Pratyush