2

How do I simply add a css class or inline style to a button like this? Is there something you can put in that array() where I got label?

private function createDeleteForm($id)
{
    return $this->createFormBuilder()
        ->setAction($this->generateUrl('video_delete', array('id' => $id)))
        ->setMethod('DELETE')
        ->add('submit', 'submit', array('label' => 'Radera denna video'))
        ->getForm()
    ;
}

I tried the aged old trick of looking at the elements id to see if I could sort of hack it by putting #ItsId in my css file, but no it doesn't have an id. It renders like this:

<form method="post" action="/site/web/app_dev.php/Media/video/1/delete">
    <input type="hidden" name="_method" value="DELETE">

        <div id="form">
            <div>
                <button type="submit" id="form_submit" name="form[submit]">
                    Radera denna video
                </button>
            </div>

            <input type="hidden" id="form__token" name="form[_token]" value="2c2889b6744aade419618485e1b3ce0c9c341a42">

        </div>
</form>
Matt Welander
  • 8,234
  • 24
  • 88
  • 138
  • possible duplicate of [How to set a class attribute to a Symfony2 form input](http://stackoverflow.com/questions/6734821/how-to-set-a-class-attribute-to-a-symfony2-form-input) – Darryl Hein Jun 28 '15 at 03:25

3 Answers3

3
->add('submit', 'submit', array(
    'label' => 'Radera denna video', 
    'attr' => array('class' => 'class-you-wish-to-add')
));

This is a duplicate of: How to set a class attribute to a Symfony2 form input

Community
  • 1
  • 1
Derick F
  • 2,749
  • 3
  • 20
  • 30
2
'attr'=> array('class'=>'span2')

TRy this for add an attribut ;)

Clément Andraud
  • 9,103
  • 25
  • 80
  • 158
1

Whilst you can put styles into the array using

->add('submit', 'submit', array(
    'label' => 'Radera denna video',
    'attr'  => array(
        'class' => 'your-class',
    ),
)

This, in my opinion, is the wrong place to put your styling details.

In my opinion, function should be in the Type (so if you had a bundle based javascript that used a set class then it should be set in this class) but styles should be based in the templates

{{ form_widget(form.submit, {'attr': {'class': 'your-class'}}) }}

This allows for your forms to be placed in other templates without being tied to a specific styling.

qooplmao
  • 17,622
  • 2
  • 44
  • 69