-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] add choice_filter
option to ChoiceType
#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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Form] add choice_filter
option to ChoiceType
#18334
Conversation
Basic usage: $builder->add('locales', LocaleType::class, array(
'choice_filter' => function ($choice) use ($userLocales) {
return in_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
to
476eb3e
Compare
Tests added. |
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 the CachingFactoryDecorator
, 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
to
bd476e1
Compare
bd476e1
to
16c3e02
Compare
@stof any thought on this one regarding the previous (wrong) implementation ? |
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'.
@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). |
@stof I absolutely agree ;) Thank you for your feedback! Status: Needs Work |
choice_filter
option to ChoiceType
choice_filter
option to ChoiceType
choice_filter
option to ChoiceType
choice_filter
option to ChoiceType
@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 |
I like the idea, thanks! I'll try to implement it soon when revisiting this one. |
@ogizanagi Just curious why not simply use the |
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 |
I think you can already solve this case using a custom type: class UserLocaleType extends AbstractType
{
public function configureOptions(OptionsResolver $resolver)
{
$resolver
->setRequired('display_locale')
->setNormalizer('choice_label', function (Options $options) {
$displayLocale = $options['display_locale'];
return function ($choice) use ($displayLocale) {
return Intl::getLocaleBundle()->getLocaleName($choice, $displayLocale);
};
})
;
}
public function getParent()
{
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 :) |
But regarding performances implementing a specific type like in my example may be better. |
I agree. Such uses of the |
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 |
@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. |
@HeahDude Sounds reasonable to me. Do you think you can finish this one? |
ping @HeahDude |
@HeahDude Do you think you can work on finishing this PR or shall we close it? |
Closing this PR as there is no more activity. @HeahDude Feel free to reopen whenever you have time to finish it. Thanks. |
See alternative #28607 |
ChoiceTypeTest