user2394156
user2394156

Reputation: 1840

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?

Upvotes: 8

Views: 18203

Answers (1)

SirDerpington
SirDerpington

Reputation: 11460

{{ 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.

Upvotes: 17

Related Questions