Skip to content

[Form] Deprecated usage of "choices" option in sub types #21919

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
Apr 5, 2017

Conversation

HeahDude
Copy link
Contributor

@HeahDude HeahDude commented Mar 8, 2017

Q A
Branch? master
Bug fix? no
New feature? no
BC breaks? no
Deprecations? yes
Tests pass? yes
Fixed tickets #...
License MIT
Doc PR ~

Follows #21880 and the discussion in #20771.

UPGRADE-4.0.md Outdated
@@ -184,6 +184,31 @@ Form
}
```

* Using the "choices" option in ``CountryType``, ``CurrencyType``, ``LanguageType``,
``LocaleType``, and ``TimezoneType`` is now ignored. Use the "choice_loader" option
instead or explicitly set it to ``null``.
Copy link
Member

Choose a reason for hiding this comment

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

Should we really support both ways to solve it?

Copy link
Contributor Author

@HeahDude HeahDude Mar 8, 2017

Choose a reason for hiding this comment

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

Yes I think so, if one wants to override choices, he should not be forced to override the loader.

It makes no sense IMO to enforce lazy load something that is already loaded, if you have your choices, just pass them, do not encapsulate them in an objet that will eventually call a function to return the initial thing.

Copy link
Member

@stof stof Mar 8, 2017

Choose a reason for hiding this comment

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

@xabbuh supporting a single way to achieve it would actually involve more work to forbid one of them. Both ways are just features of the ChoiceType: if choice_loader is not null, it is used. Otherwise choices is used.

The CountryType & co currently have to do extra work to ensure that this choices wins over the default choice_loader, due to BC (there was no default choice loader previously).

@@ -38,7 +38,13 @@ public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'choice_loader' => function (Options $options) {
return $options['choices'] ? null : $this;
if ($options['choices']) {
@trigger_error(sprintf('Using "choices" option in %s has been deprecated since version 3.3 and will be ignored in 4.0. Override the "choice_loader" option instead.', __CLASS__), E_USER_DEPRECATED);
Copy link
Member

Choose a reason for hiding this comment

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

Either we should only support the choice_loader solution in the future or talk about the possibility to explicitly set it to null here too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I hesitated, considering your comments I'd rather add it explicitly here too, thanks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, I've just updated the notices.

@@ -38,7 +38,13 @@ public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'choice_loader' => function (Options $options) {
return $options['choices'] ? null : $this;
if ($options['choices']) {
@trigger_error(sprintf('Using "choices" option in %s has been deprecated since version 3.3 and will be ignored in 4.0. Override the "choice_loader" option instead or set it to null.', __CLASS__), E_USER_DEPRECATED);
Copy link
Member

Choose a reason for hiding this comment

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

Using the "choices" option [...]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed everywhere.

public function testChoiceLoaderIsOverridden($type)
{
$factory = Forms::createFormFactoryBuilder()
->addTypeExtension(new LazyChoiceTypeExtension($type))
Copy link
Member

Choose a reason for hiding this comment

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

Do we really need the type extension here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would keep it like it for consistency with the test above, and because it test both overriding the option directly and doing it with an extension, so looks safer as is.

@nicolas-grekas nicolas-grekas added this to the 3.3 milestone Mar 10, 2017
@HeahDude HeahDude force-pushed the bc/choice-types branch 2 times, most recently from 1477302 to 64ddfa1 Compare March 26, 2017 20:52
@HeahDude
Copy link
Contributor Author

Comments addressed and rebased. Ready for final review before merging, ping @symfony/deciders.

UPGRADE-3.3.md Outdated
Form
------

* Using the "choices" option in ``CountryType``, ``CurrencyType``, ``LanguageType``,
Copy link
Member

Choose a reason for hiding this comment

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

I think we need to reword this a bit to make clear that this is only deprecated as long as you do not set the choice_loader option to null (something like "using the choices option without overriding the choice_loader option is deprecated").

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea! Done.

Copy link
Member

@xabbuh xabbuh left a comment

Choose a reason for hiding this comment

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

👍

```php
$builder->add('custom_locales', LocaleType::class, array(
'choices' => $availableLocales,
'choice_loader' => null,
Copy link
Contributor

Choose a reason for hiding this comment

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

That doesn't look intuitive to me.

Copy link
Member

Choose a reason for hiding this comment

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

Well, this is because choice_loader has priority over choices in ChoiceType (which is not documented IIRC btw)

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 this should not be used ideally, the ChoiceType is more accurate in such cases, I'll update the doc to clarify this.

Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't it be "better" to drop this example and just keep the second code snippet? And remove the recommendation to use choice_loader as null?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I understand this does not look pretty, but this is the actual quick fix for some breaking app.

What we can't do is to stop supporting "choices" option as it's inherited from the ChoiceType, and in such cases null is needed because of its precedence.

What we can do is to better document these types options, and explicit that "choices" or "choice_loader" options should be overridden on a ChoiceType or an EntityType only, not on a child already doing so, unless the previous value is reused.

I would like to address your comment, but as I said in a previous outdated diff:

It makes no sense IMO to enforce lazy load something that is already loaded, if you have your choices, just pass them, do not encapsulate them in an objet that will eventually call a function to return the initial thing.

Then the real fix would be to use the ChoiceType (if the previous "choices" values is not used), I could add this instead, but objectively I would let it as is now.

I'm waiting for your final call on this symfony deciders.

Copy link
Member

Choose a reason for hiding this comment

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

I am fine with how it is now. But we should indeed clarify the documentation about the differences of the choices and choice_loader options and when to use what.

@fabpot
Copy link
Member

fabpot commented Apr 5, 2017

Thank you @HeahDude.

@fabpot fabpot merged commit b5b56fc into symfony:master Apr 5, 2017
fabpot added a commit that referenced this pull request Apr 5, 2017
…es (HeahDude)

This PR was merged into the 3.3-dev branch.

Discussion
----------

[Form] Deprecated usage of "choices" option in sub types

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | yes
| Tests pass?   | yes
| Fixed tickets | #... <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | ~

Follows #21880 and the discussion in #20771.

Commits
-------

b5b56fc [Form] Deprecated usage of "choices" option in sub types
@HeahDude HeahDude deleted the bc/choice-types branch April 5, 2017 21:00
@HeahDude HeahDude restored the bc/choice-types branch April 23, 2017 08:39
@HeahDude HeahDude deleted the bc/choice-types branch April 23, 2017 08:39
@fabpot fabpot mentioned this pull request May 1, 2017
xabbuh added a commit to symfony/symfony-docs that referenced this pull request Jun 10, 2017
This PR was merged into the 3.3 branch.

Discussion
----------

Explain how to properly override choices

Update the documentation for symfony/symfony#21919. This fixes #7779.

Commits
-------

a9f934e explain how to properly override choices
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