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

Update multiple_buttons.rst #12089

merged 1 commit into from
Sep 20, 2019

Conversation

Romaxx
Copy link
Contributor

@Romaxx Romaxx commented Aug 5, 2019

Reference to symfony/symfony#32932

@@ -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"?

@OskarStark
Copy link
Contributor

@Romaxx would you like to finish this PR?

@javiereguiluz javiereguiluz changed the base branch from 4.3 to 3.4 September 20, 2019 18:00
javiereguiluz added a commit that referenced this pull request Sep 20, 2019
This PR was submitted for the 4.3 branch but it was merged into the 3.4 branch instead (closes #12089).

Discussion
----------

Update multiple_buttons.rst

Reference to symfony/symfony#32932

Commits
-------

194bca1 Update multiple_buttons.rst
@javiereguiluz javiereguiluz merged commit 194bca1 into symfony:3.4 Sep 20, 2019
@javiereguiluz
Copy link
Member

@Romaxx thanks for contributing this improvement. I agreed both with you and @xabbuh so I reworded this as follows while merging:

    // when using nested forms, two or more buttons can have the same name;
    // in those cases, compare the button objects instead of the button names
    if ($form->getClickedButton() === $form->get('saveAndAdd')){
        // ...
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants