The first time I read about this feature, I was sceptical at best. Why would you want to manage buttons in your forms as buttons are only a display thing? I was wrong and once more, Bernhard did a great job implementing this new feature.
As of Symfony 2.3, you can add buttons to your forms, like any other fields:
1 2 3 4
$form = $this->createFormBuilder($task)
->add('name', 'text')
->add('save', 'submit')
->getForm();
When rendering the form with the canonical {{ form_widget(form) }}
instruction, the button will be rendered as well. But that alone would not be
very interesting. So, what is it really about?
Submit buttons in forms are not really useful except for when there are more than one; the logic that processes the form will probably be different depending on the button that was clicked by the user:
1 2 3 4 5
$form = $this->createFormBuilder($task)
->add('name', 'text')
->add('save', 'submit')
->add('save_and_add', 'submit')
->getForm();
And that's a situation where managing buttons in the form framework starts to
make sense. From your controller, you can now check which button was clicked
with the new isClicked()
method and act accordingly:
1 2 3 4 5 6 7 8 9 10
if ($form->isValid()) {
// ... do something
// the save_and_add button was clicked
if ($form->get('save_and_add')->isClicked()) {
// probably redirect to the add page again
}
// redirect to the show page for the just submitted item
}
But there is more than that: the validation group can be different for each button. That comes in handy when working with wizard forms:
1 2 3 4 5 6 7 8
$form = $this->createFormBuilder($task)
// ...
->add('nextStep', 'submit')
->add('previousStep', 'submit', array(
// disable validation
'validation_groups' => false,
))
->getForm();
Of course, this feature is entirely optional and for forms with only one submit button, you can keep the current way of doing things.
This new feature is fully documented in the official documentation; but having a look at the documentation pull request might be more convenient.
Yessss! Finally! Thanks a lot!
This is actually very good and logical choice. I was missing this feature. Thanks.
Awesome! Thanks.
Very nice, well played ! ;)
Yeah. Finally. Thx
Really loving this new feauture, It felt really messy the need to define the fields in one place and the buttons in the view
Amazing feature!
Great Contributtion!!
it's very useful ! i like it (y) :)
Nice
Oh, it's and interesting feature thanks a lot for this!
finally, completly just in one file
Thanks
That's a cool one keep it up
When will this be available through the standalone Form component (composer) ?
I really missed this feature! Wonderful addition, thanks!
This changes works in Silex ?
Thanks !
Interesting, but like your first sentence reads, I am skeptical at best that this is a good idea. While I do see the value in using this feature with multiple submit buttons as stated in the example, that still doesn't explain why the "button" or "reset" type was added and what the purpose is of using those.
Great feature. I use it now :D
sounds cool.