4

I'm using the Bootstrap form theme for Symfony2 (bootstrap_3_horizontal_layout.html.twig):

I've added two buttons on a form:

$builder
    // some code here ... 
    ->add('save', 'submit', ['label' => 'save'])
    ->add('cancel', 'submit', ['label' => 'cancel']);

And they are rendered this way: enter image description here

I need that they be situated on the same row: enter image description here

How can it be achieved?

Vasily
  • 1,858
  • 1
  • 21
  • 34

3 Answers3

13

Following the

Symfony Best Practises: Add buttons in the templates, not in the form classes or the controllers.

E.g.:

{{ form_start(form) }}
  {{ form_widget(form) }}

  <div class="row">
    <div class="col-sm-offset-2 col-sm-10">
      <button type="submit" value="save" class="btn btn-primary">save</button>
      <button type="submit" value="cancel" class="btn btn-default">cancel</button>
    </div>
  </div>
{{ form_end(form) }}
Yoshi
  • 54,081
  • 14
  • 89
  • 103
  • Is it still possible in controller to know which button was pushed when adding buttons only in templates ? $form->get('save') throws an OutOfBoundsException exception, $form->getData() doesn't have the information anymore. ie: http://symfony.com/blog/new-in-symfony-2-3-buttons-support-in-forms – Patrick Nov 13 '15 at 10:18
  • 2
    @Patrick I know your problem, and honestly I tripped over it myself a few times. The purpose of the best practice advice is to keep the form reusable, yet context-sensitive buttons would destory such purpose. If you still need them in the controller (which sometimes really is beneficial), you could for example: a) extend the base form to only add the needed buttons, or b) add the buttons in the controller after creating the form object. – Yoshi Nov 13 '15 at 10:25
3

Just add a style float left to the save button. This works for me:

    ->add('save', 'submit', ['label' => 'save', 'attr' => array('style' => 'float: left')])
    ->add('cancel', 'submit', ['label' => 'cancel']);
Matteo
  • 37,680
  • 11
  • 100
  • 115
  • Thanks. Do you use Bootstrap theme too? I've tried this, but have no result, because of the bootstrap internals - the style is applied to a button tag, but the button by itself is situated in
    tag, which is created for each button.
    – Vasily Aug 06 '15 at 09:04
  • i use the form theming described in the news link you posted and make a simple form with two text field and your two button. Then i render in the template as `{{ form(form) }}` and use the `{% form_theme form 'bootstrap_3_layout.html.twig' %}` in my page, every button are into a `
    `
    – Matteo Aug 06 '15 at 09:08
  • Oh I'm sorry, I forgot to inform that I'm using "bootstrap_horizontal_layout.html.twig", in which your recipe somehow doesn't work, but works perfectly with "bootstrap_layout.html.twig" – Vasily Aug 06 '15 at 09:15
  • 1
    It won't work in applying to buttons. You can set style "display:inline-block;" to both buttons or wrap them with left floated divs. And as Yoshi suggested below it'sbetter to define submit buttons in templates (but bear in mind it's only recommendation). In template you can much easier manipulate with html elements' attributes. – Alex Aug 06 '15 at 09:48
-7

Have you tried to put the 2nd line on the same line as the first?

Like so:

->add('save', 'submit', ['label' => 'save']) ->add('cancel', 'submit', ['label' => 'cancel']);
Max Nijholt
  • 3
  • 1
  • 7