Skip to content

Update multiple_buttons.rst #12089

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 20, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions form/multiple_buttons.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@ Or you can get the button's name by using the
if ($form->getClickedButton() && 'saveAndAdd' === $form->getClickedButton()->getName()) {
// ...
}

If you have multiple buttons with the same name, each nested in multiple subforms, use direct reference instead the name to get the right button
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would clarify here that this is related to a form name used multiple times. And we could also just add it inside the code block above. Maybe something like this:

Or you can get the button's name by using the
:method:`Symfony\\Component\\Form\\Form::getClickedButton` method of the form::

    if ($form->getClickedButton() && 'saveAndAdd' === $form->getClickedButton()->getName()) {
        // ...
    }

    // if you have multiple buttons with the same name, compare the object references instead
    if ($form->getClickedButton() === $form->get('saveAndAdd')){
        // ...
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes but multiple buttons with the same name in the same form is not possible, so I wanted to clarify that it was in nested form situation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe using @xabbuh's proposal + a small note about the "same name"?


if ($form->getClickedButton() && $form->getClickedButton() === $form->get('saveAndAdd')){

}