Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[Form] addchoice_filter option toChoiceType#18334
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
[Form] addchoice_filter option toChoiceType#18334
Uh oh!
There was an error while loading.Please reload this page.
Conversation
HeahDude commentedMar 28, 2016
Basic usage: $builder->add('locales', LocaleType::class,array('choice_filter' =>function ($choice)use ($userLocales) {returnin_array($choice,$userLocales,true); },));$builder->add('entities', EntityType::class,array('class' => \AppBundle\Entity\Issue::class,'choice_filter' =>function ($choice)use ($user) {return$user->canHandle($choice); },)); |
fac72f6 to476eb3eCompareHeahDude commentedMar 28, 2016
Tests added. |
HeahDude commentedMar 28, 2016
This is a full BC implementation. The only thing that should be noted in UPGRADE is that someone uses its own |
| { | ||
| // Don't hash the filter since the original choices may have been loaded already | ||
| // with a different filter if any. | ||
| $hash = CachingFactoryDecorator::generateHash(array($loader, $value)); |
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 just not sure about the over caching here, but it seems that considering this decorator might not decorate theCachingFactoryDecorator, keeping the resolved choices to filter should be the right thing to do.
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.
This caching is useless, as the decorated choice list already has caching
476eb3e tobd476e1Comparebd476e1 to16c3e02CompareHeahDude commentedMar 29, 2016
@stof any thought on this one regarding the previous (wrong) implementation ? |
HeahDude commentedMar 30, 2016
ping@webmozart :) |
| /** | ||
| * @dataProvider provideChoicesAndFilter | ||
| */ | ||
| public function testCreateFilteredChoiceListFromLoaderLoadsOriginalListOnFirstCall($choices, $choiceList, $filter, $filteredChoices) |
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.
Issue found:Avoid unused parameters such as '$choices'.
stof commentedApr 7, 2016
@HeahDude this will have to wait until 3.2. We are already in the 3.1 feature freeze, and there is still too much to evaluate about this feature to merge it now (we are not yet fully freezed as we allow us to include some PRs which are ready but not yet merged, but this feature cannot be considered as ready yet IMO). |
HeahDude commentedApr 7, 2016
@stof I absolutely agree ;) Thank you for your feedback! Status: Needs Work |
choice_filter option toChoiceTypechoice_filter option toChoiceTypechoice_filter option toChoiceTypechoice_filter option toChoiceTypeogizanagi commentedDec 11, 2016
@HeahDude : What about accepting an array in the The following: $builder->add('locales', LocaleType::class,array('choice_filter' =>$userLocales,)); will automatically generate the callable using |
HeahDude commentedDec 11, 2016
I like the idea, thanks! I'll try to implement it soon when revisiting this one. |
HeahDude commentedDec 11, 2016
@ogizanagi Just curious why not simply use the |
ogizanagi commentedDec 11, 2016 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
I expected this question 😅 This allows to blindly filter the available values, without caring of the implementation of the type. In a config file, you only want to use Using the |
HeahDude commentedDec 11, 2016 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
I think you can already solve this case using a custom type: class UserLocaleTypeextends AbstractType{publicfunctionconfigureOptions(OptionsResolver$resolver) {$resolver ->setRequired('display_locale') ->setNormalizer('choice_label',function (Options$options) {$displayLocale =$options['display_locale'];returnfunction ($choice)use ($displayLocale) {return Intl::getLocaleBundle()->getLocaleName($choice,$displayLocale); }; }) ; }publicfunctiongetParent() {return ChoiceType::class; }}// Then you can do$builder->add('locales', UserLocaleType::class,array('choices' =>$userLocales,'display_locale' =>$userLocale,)); But I agree, the filter option definitely simplifies the implementation though :) |
HeahDude commentedDec 11, 2016
But regarding performances implementing a specific type like in my example may be better. |
ogizanagi commentedDec 11, 2016 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
I agree. Such uses of the |
xabbuh commentedDec 18, 2016
Is this really something that will be commonly used? I mean if you need to filter your choices, why don't you just do that when specifying the |
HeahDude commentedDec 18, 2016
@xabbuh in case of inheritance there would be no need to define again the For the So to me this feature is the right answer to both needs. |
xabbuh commentedDec 30, 2016
@HeahDude Sounds reasonable to me. Do you think you can finish this one? |
nicolas-grekas commentedSep 26, 2017
ping@HeahDude |
fabpot commentedJan 24, 2018
@HeahDude Do you think you can work on finishing this PR or shall we close it? |
fabpot commentedMar 31, 2018
Closing this PR as there is no more activity.@HeahDude Feel free to reopen whenever you have time to finish it. Thanks. |
yceruto commentedSep 26, 2018
See alternative#28607 |
ChoiceTypeTest