Skip to content
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

[Form] getClickedButton() missing in FormInterface returned by builder #35277

Closed
ThomasLandauer opened this issue Jan 9, 2020 · 4 comments
Closed
Labels

Comments

@ThomasLandauer
Copy link
Contributor

Symfony version(s) affected: 4.4

phpstan reports:

Call to an undefined method Symfony\Component\Form\FormInterface::getClickedButton()

According to @althaus this is because getClickedButton() is not included in the FormInterface returned by the builder, see phpstan/phpstan-symfony#12 (comment)

@ThomasLandauer
Copy link
Contributor Author

And when trying it the other way as explained at https://symfony.com/doc/current/form/multiple_buttons.html

$form->get('saveAndAdd')->isClicked()

... I'm getting:

Call to an undefined method Symfony\Component\Form\FormInterface::isClicked()

@xabbuh xabbuh added the Form label Jan 9, 2020
@xabbuh
Copy link
Member

xabbuh commented Jan 9, 2020

getClickedButton() is not part of the interface as it wouldn't make much sense for a Button implementation which also implements the FormInterface. Likewise isClicked() is not part of the interface as it is only useful for buttons.

If you want to stop PHPStan complaining about it without adding it to the baseline, you can do something like this:

if ($form instanceof Form) {
    $button = $form->getClickedButton();
}

// or

/** @var Form $form */
$button = $form->getClickedButton();

I don't think there is anything we can do in the Form component about this.

@ro0NL
Copy link
Contributor

ro0NL commented Jan 9, 2020

at least SF anticipates each form implementation may implement this:

if (method_exists($form, 'getClickedButton')) {
$clickedButton = $form->getClickedButton();
}

which to me is a hint it's missing in some contract, somewhere

@xabbuh
Copy link
Member

xabbuh commented Jan 10, 2020

@ro0NL Technically there could be other implementations of the FormInterface. So the FormValidator takes the safe path and performs this check first to prevent type errors even if it's probably always true in a typical Symfony application.

I am closing as explained in #35277 (comment).

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

No branches or pull requests

3 participants