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 | $form = $this->createFormBuilder($task) |
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 | $form = $this->createFormBuilder($task) |
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 | if ($form->isValid()) { |
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 | $form = $this->createFormBuilder($task) |
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.
No comments:
Post a Comment