Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Form tag attributes in Symfony2 / Twig

When I want to add some attributes to symfony2 form elements I can just use the "attr" attribute. But how can I give some id / class / style / other to the starting form tag itself?

like image 387
user2394156 Avatar asked Jul 10 '13 19:07

user2394156


1 Answers

{{ form_widget(form.name, { 'attr': {'class': 'foo'} }) }}

This is the correct syntax for a form widget with the class foo. In other words a form field in your form.

You can apply the same for the <form> tag.

This is done by rendering

{{ form_start(form, { 'attr': {'class': 'foo', 'id': 'bar', ... } }) }}

See this short documentation on form_start here

Renders the start tag of a form. This helper takes care of printing the configured method and target action of the form. It will also include the correct enctype property if the form contains upload fields.

You might also be interested in form variables. They are documented here. All in all there is a whole documentation on all functions and variables you can use here

I wouldn't recommend you to use inline styles. Just do the styling with the ID and/or class you gave the form.

like image 55
SirDerpington Avatar answered Oct 05 '22 08:10

SirDerpington