Skip to content

[Form] Deprecate a callable empty_data in ChoiceType #25924

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

Conversation

peter-gribanov
Copy link
Contributor

@peter-gribanov peter-gribanov commented Jan 25, 2018

Q A
Branch? 4.1
Bug fix? no
New feature? no
BC breaks? no
Deprecations? yes
Tests pass? yes
License MIT

This PR is related to bug #25896, but doesn't solve it.

@peter-gribanov peter-gribanov changed the title Empty data callable deprecated [Form] Empty data callable deprecated Jan 25, 2018
@peter-gribanov peter-gribanov changed the title [Form] Empty data callable deprecated [Form] Deprecate a callable empty_data in ChoiceType Jan 25, 2018
@xabbuh xabbuh added this to the 4.1 milestone Jan 25, 2018
@xabbuh xabbuh added the Form label Jan 25, 2018
UPGRADE-5.0.md Outdated
```php
'empty_data' => function (FormInterface $form, $data) {
return SomeValueObject::getDefaultValue();
},
Copy link
Member

@derrabus derrabus Jan 25, 2018

Choose a reason for hiding this comment

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

I think, you should rather use a global function call as an example. A static method call can still be written as ['SomeValueObject', 'getDefaultValue'] and I personally would prefer that notation over an anonymous function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

And in vain you prefer such a call to static functions.
This leads to the fact that the connection to the real function is lost and this is guaranteed to lead to problems during refactoring.

Copy link
Member

Choose a reason for hiding this comment

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

Well, it was not my intention to discuss my personal preferences. Shouldn't have mentioned it. Still, the static call adds unnecessary distraction to the example, imho.

UPGRADE-5.0.md Outdated
Form
----

* Using callable strings as `empty_data` in `ChoiceType` has been deprecated in Symfony 4.1 use a `\Closure` instead.
Copy link
Member

Choose a reason for hiding this comment

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

Should be:

* Support for callable strings as `empty_data` in `ChoiceType` has been removed. Use a `\Closure` instead.

UPGRADE-4.1.md Outdated
Form
----

* Using callable strings as `empty_data` in `ChoiceType` has been deprecated in Symfony 4.1 use a `\Closure` instead.
Copy link
Member

Choose a reason for hiding this comment

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

Better: … is deprecated and will be removed in Symfony 5.0, use a…

4.1.0
-----

* Using callable strings as `empty_data` in `ChoiceType` has been deprecated in Symfony 4.1 use a `\Closure` instead.
Copy link
Member

Choose a reason for hiding this comment

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

The version information is redundant. To match the message style of other entries in this file, I'd suggest:

* deprecated support for callable strings as `empty_data` in `ChoiceType`

@derrabus
Copy link
Member

Maybe we should add FileType (as mentioned in #25896 (comment)) to this PR. It seems odd to deprecate that mechanism for one type, but leave it active for another.

@@ -91,7 +91,14 @@ public function buildForm(FormBuilderInterface $builder, array $options)
$emptyData = $form->getConfig()->getEmptyData();

if (false === FormUtil::isEmpty($emptyData) && array() !== $emptyData) {
$data = is_callable($emptyData) ? call_user_func($emptyData, $form, $data) : $emptyData;
if (is_callable($emptyData)) {
if (is_string($emptyData)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I've introduced that change as a bug fix in #17822 targeting 2.7.
But I've made a mistake, IMO we should do it like https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Form.php#L610 and merge it in 2.7 as bug fix too. No need to deprecate anything.

Copy link
Member

Choose a reason for hiding this comment

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

#17822 is over 1.5y old now. People might have built code against that "feature" already. :-(

Copy link
Member

Choose a reason for hiding this comment

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

Also, I would not check for instanceof \Closure as this would forbid array callables and invokable objects for no reason.

Copy link
Contributor

Choose a reason for hiding this comment

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

The empty_data MUST be a closure for all forms, I'm not sure people rely on this hidden feature which is just a bug I introduced no matter when IMO.

@peter-gribanov
Copy link
Contributor Author

Maybe we should add FileType (as mentioned in #25896 (comment)) to this PR.

@derrabus sorry, but i would prefer to see this in another PR.

After:

```php
'empty_data' => function (FormInterface $form, $data) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Or just Closure::fromCallable('some_function') 😉

@fabpot
Copy link
Member

fabpot commented Feb 1, 2018

Closing in favor of #25948

@fabpot fabpot closed this Feb 1, 2018
fabpot added a commit that referenced this pull request Feb 1, 2018
…e (HeahDude)

This PR was merged into the 2.7 branch.

Discussion
----------

[Form] Fixed empty data on expanded ChoiceType and FileType

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #25896
| License       | MIT
| Doc PR        | ~

Alternative of #25924.

I don't think we have to do this by adding overhead in master and getting it properly fixed in 2 years.

This is bug I've introduced while fixing another bug #17822. Which then has been replicated in #20418 and #24993.

I propose instead to clean up the code for all LTS, since this is a wrong behaviour that has never been documented, and most of all unreliable.
The `empty_data` can by anything in the view format as long as the reverse view transformation supports it. Even an object derived from the data class could be invokable.

I think we should remain consistent with https://github.com/symfony/symfony/blob/2.7/src/Symfony/Component/Form/Form.php#L615.

Commits
-------

9722c06 [Form] Fixed empty data on expanded ChoiceType and FileType
symfony-splitter pushed a commit to symfony/form that referenced this pull request Feb 1, 2018
…e (HeahDude)

This PR was merged into the 2.7 branch.

Discussion
----------

[Form] Fixed empty data on expanded ChoiceType and FileType

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #25896
| License       | MIT
| Doc PR        | ~

Alternative of symfony/symfony#25924.

I don't think we have to do this by adding overhead in master and getting it properly fixed in 2 years.

This is bug I've introduced while fixing another bug #17822. Which then has been replicated in #20418 and #24993.

I propose instead to clean up the code for all LTS, since this is a wrong behaviour that has never been documented, and most of all unreliable.
The `empty_data` can by anything in the view format as long as the reverse view transformation supports it. Even an object derived from the data class could be invokable.

I think we should remain consistent with https://github.com/symfony/symfony/blob/2.7/src/Symfony/Component/Form/Form.php#L615.

Commits
-------

9722c063fb [Form] Fixed empty data on expanded ChoiceType and FileType
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.

7 participants