-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
[Form] Adding callable
to choices
#21003
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Callable is not allowed in
ChoiceType
option resolverhttps://github.com/symfony/symfony/blob/db8b9da89978110c7b1b299e2218dbe6abcbef4e/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php#L380
What is the return type of
MyEnum::myChoices()
in your comment ?https://github.com/symfony/symfony/blob/db8b9da89978110c7b1b299e2218dbe6abcbef4e/src/Symfony/Component/Form/Extension/Core/Type/EnumType.php#L32C95-L32C100
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.
An array of enum cases:
I didn't look at the source code; just tried if it works. I'm getting only those 2 radiobuttons, but the array keys aren't used as label.
I just opened another issue about this yesterday: #21436
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.
If I understand what you did here correctly, your configuration is relying on the built-in feature of the OptionsResolver component to define default options depending on another option: https://symfony.com/doc/current/components/options_resolver.html#default-values-that-depend-on-another-option
I don't think we should cover this by adding
callable
everywhere as the allowed 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.
I'm not sure if I get what you mean.
What I want is to define the choices not directly in the form class (as @javiereguiluz shows it in fe70fe0), but rather define a function in the enum.
So this is not really supported, and works just cause I have luck?
Second: Looking at Javier's docs PR again: Am I right that adding array keys to be used as form labels will never work with
enumType
? If so, I think this should be added there - something like:=> Is this right?
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.
If that's not possible, we should fix it in the code. Can you open a bug report please?
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.
Sure, there you go :-) symfony/symfony#61927
But for the first question again: So is using a callable OK or not?
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.
Re-reading your PR description I am not sure you really do pass a callback to
choices
option.Is this how you do 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, that's how I do it.
And I now see what you mean :-) I was thinking that a function that returns an array counts as
callable
. But obviouslycallable
is only meant for a "real" callback function that receives some arguments.So if you agree, this PR can be closed.
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.
By the way, I submitted a PR that is supposed to fix the issue you reported. Please give it a try. :)