-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Conversation
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``. |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
bbdba78
to
7ba98df
Compare
@@ -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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using the "choices" option [...]
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
1477302
to
64ddfa1
Compare
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``, |
There was a problem hiding this comment.
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").
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea! Done.
64ddfa1
to
b5b56fc
Compare
There was a problem hiding this 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, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Thank you @HeahDude. |
…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
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
Follows #21880 and the discussion in #20771.