Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[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

Conversation

@HeahDude
Copy link
Contributor

QA
Branch?master
Bug fix?no
New feature?yes
BC breaks?no
Deprecations?no
Tests pass?yes
Fixed tickets#11847,#14072
LicenseMIT
Doc PRtodo
  • Add some tests
  • Add more tests inChoiceTypeTest
  • Address a doc PR
  • Update the CHANGELOG
  • Add a note in the UPGRADE file

ogizanagi and yceruto reacted with thumbs up emojiDavidBadura reacted with hooray emoji
@HeahDude
Copy link
ContributorAuthor

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);    },));

@HeahDudeHeahDudeforce-pushed thefeature-choice_type-choice_filter-option branch 5 times, most recently fromfac72f6 to476eb3eCompareMarch 28, 2016 15:08
@HeahDude
Copy link
ContributorAuthor

Tests added.

@HeahDude
Copy link
ContributorAuthor

This is a full BC implementation.

The only thing that should be noted in UPGRADE is that someone uses its ownChoiceListFactory should either implementFilteredChoiceListFactoryInterface or use theFilteringFactoryDecorator to use the newchoice_filter option, otherwise it will simply be ignored.

{
// 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));
Copy link
ContributorAuthor

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.

Copy link
Member

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

@HeahDudeHeahDudeforce-pushed thefeature-choice_type-choice_filter-option branch from476eb3e tobd476e1CompareMarch 28, 2016 15:55
@HeahDude
Copy link
ContributorAuthor

@stof any thought on this one regarding the previous (wrong) implementation ?

@HeahDude
Copy link
ContributorAuthor

ping@webmozart :)

/**
* @dataProvider provideChoicesAndFilter
*/
public function testCreateFilteredChoiceListFromLoaderLoadsOriginalListOnFirstCall($choices, $choiceList, $filter, $filteredChoices)

Choose a reason for hiding this comment

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

@stof
Copy link
Member

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).
So I will focus on the review of other PRs for now (the ones still considered for inclusion in 3.1) and come back at this feature later.

@HeahDude
Copy link
ContributorAuthor

@stof I absolutely agree ;) Thank you for your feedback!

Status: Needs Work

@HeahDudeHeahDude changed the title[Form] addchoice_filter option toChoiceType[WIP] [3.2] [Form] addchoice_filter option toChoiceTypeApr 28, 2016
@nicolas-grekasnicolas-grekas changed the title[WIP] [3.2] [Form] addchoice_filter option toChoiceType[Form] addchoice_filter option toChoiceTypeDec 6, 2016
@nicolas-grekasnicolas-grekas added this to the3.x milestoneDec 6, 2016
@ogizanagi
Copy link
Contributor

@HeahDude : What about accepting an array in thechoice_filter option?

The following:

$builder->add('locales', LocaleType::class,array('choice_filter' =>$userLocales,));

will automatically generate the callable usingin_array.
It'll help when the type is configurable from a configuration file for instance (like in EasyAdminBundle).

HeahDude reacted with thumbs up emoji

@HeahDude
Copy link
ContributorAuthor

will automatically generate the callable using in_array.

I like the idea, thanks! I'll try to implement it soon when revisiting this one.

@HeahDude
Copy link
ContributorAuthor

@ogizanagi Just curious why not simply use thechoices option of theChoiceType in this case?

@ogizanagi
Copy link
Contributor

ogizanagi commentedDec 11, 2016
edited
Loading

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%locales% for instance (which only contains values).
Behind, the type has the responsibility to associate the proper labels for instance.

Using thechoices option of theChoiceType forces you to redefine everything.

@HeahDude
Copy link
ContributorAuthor

HeahDude commentedDec 11, 2016
edited
Loading

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
Copy link
ContributorAuthor

But regarding performances implementing a specific type like in my example may be better.

@ogizanagi
Copy link
Contributor

ogizanagi commentedDec 11, 2016
edited
Loading

I agree. Such uses of thechoice_filter option will be reserved mainly for countries, locales, languages (...). I doubt it'll have a big impact on performances, but it has a very good one on DX.

@xabbuh
Copy link
Member

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 thechoices option then?

@HeahDude
Copy link
ContributorAuthor

@xabbuh in case of inheritance there would be no need to define again thechoices and we could use the same cached choice list on which to apply the filter.

For theEntityType it would allow to filterchoices thanks to dynamic properties or method that could not be used with a custom query builder.

So to me this feature is the right answer to both needs.

@xabbuh
Copy link
Member

@HeahDude Sounds reasonable to me. Do you think you can finish this one?

@nicolas-grekas
Copy link
Member

ping@HeahDude

@nicolas-grekasnicolas-grekas modified the milestones:3.4,4.1Oct 8, 2017
@fabpot
Copy link
Member

@HeahDude Do you think you can work on finishing this PR or shall we close it?

@fabpot
Copy link
Member

Closing this PR as there is no more activity.@HeahDude Feel free to reopen whenever you have time to finish it. Thanks.

@yceruto
Copy link
Member

See alternative#28607

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Projects

None yet

Milestone

4.1

Development

Successfully merging this pull request may close these issues.

10 participants

@HeahDude@stof@ogizanagi@xabbuh@nicolas-grekas@fabpot@yceruto@trousers@javiereguiluz@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp