Reputation: 1241
I've just started a new Symfony2 project where I've used the generate:doctrine:crud
to quickly scale out a few views, forms, etc.
The generated form code is just:
{{ form(form) }}
but includes a generic create or delete button. I was wondering how I could add a class to these generic buttons or modify them in any way since it's just encompassed in {{ form(form) }}
?
For reference I'm using Twitter Bootstrap to quickly apply some styles so I don't want to change the css based on the submit button.
Thanks!
Upvotes: 0
Views: 1831
Reputation: 611
After following dmnptr's answer (breaking form into parts), you can pass an array of arguments to each form
/ form_row
/ form_label
etc by:
{{ form(form, { 'attr': { 'class': 'your-css-class-1 your-css-class-2' } } ) }}
The attr
param sets attributes for the item, so the above would produce:
<form class="your-css-class-1 your-css-class-2" ...
Upvotes: 1
Reputation: 539
You can specify CSS classes in the form builder class to avoid filling your Twig template with html even for rendering the form individually.
When you call {{ form(form) }}
you are using a helper to simplify your code so you don't have to call form_widget
for each one of your fields, but doing so you can't control the exact display in the template. To do it you have to specify the class that will be applied to the field.
In the WhateverType.php
file, inside the Forms
folder, you have the form builder. There you should have something like:
$builder
->add('text')
->add('whatever')
There you have to add the classes:
$builder
->add('text', 'attr'=> array('class'=>'btn')
->add('whatever')
Then, when your form is displayed in the template, it will apply the classes that you specified in the builder.
Upvotes: 1
Reputation: 4304
You are going to have to render each filed by hand and add necessary classes to elements in your TWIG
. Read on how to do it in the official docs - http://symfony.com/doc/current/book/forms.html#rendering-each-field-by-hand
Upvotes: 0