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][DX] Add choice_filter option to ChoiceType#28607

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

Closed
yceruto wants to merge1 commit intosymfony:masterfromyceruto:choice_filter

Conversation

@yceruto
Copy link
Member

QA
Branch?master
Bug fix?no
New feature?yes
BC breaks?no
Deprecations?no
Tests pass?yes
Fixed tickets#11847,#14072
LicenseMIT
Doc PRTODO

Alternative of#18334 and#28542.

This is what we can do with this new option, before#28542 (comment), after#28542 (comment):

$builder->add('locale', LocaleType::class, ['choice_filter' =>function ($choice)use ($supportedLocales) {return\in_array($choice,$supportedLocales,true);    },]);// or simply$builder->add('locale', LocaleType::class, ['choice_filter' =>$supportedLocales,// which does exactly the same as above]);

In addition, we can use it withEntityType, to filter choices thanks to dynamic properties or methods that could not be used with a custom query builder.

$builder->add('issues', EntityType::class, ['class' => Issue::class,'choice_filter' =>function (Issue$issue)use ($user) {return$user->canHandle($issue);    },]);

This approach would filter the choices data just after loaded. This way, even filtered choice lists would be properly cached.

ro0NL, Hanmac, yceruto, tolry, and apfelbox reacted with hooray emojiOskarStark and yceruto reacted with heart emoji
Copy link
Contributor

@ro0NLro0NL left a comment

Choose a reason for hiding this comment

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

very nice 👌

privatefunctioncreateChoiceList(array$options)
{
if (null !==$options['choice_loader']) {
if (null !==$options['choice_filter'] &&$options['choice_loader']instanceof ChoiceFilterInterface) {
Copy link
Contributor

Choose a reason for hiding this comment

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

i think something should happen when the choice loader does not supportChoiceFilterInterface. E.g. wrap it in a decorating / callback loader, or throw.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

yeah, I think throwing an exception is a good DX 👍

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Decorating is not an option, because we lose the filtered list or the custom logic insideloadValuesForChoices andloadChoicesForValues in the decorated loader.

// Harden against NULL values (like in EntityType and ModelType)
$choices =null !==$options['choices'] ?$options['choices'] :array();

if (null !==$options['choice_filter']) {
Copy link
Contributor

Choose a reason for hiding this comment

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

&& $choices? Not sure it matters much... =/

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

sure, should save some useless function calls 👍

/**
* {@inheritdoc}
*/
publicfunctionsetChoiceFilter(callable$choiceFilter)
Copy link
Contributor

Choose a reason for hiding this comment

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

Wild thoughts, as I don't know all the implications right now: I don't know if a new interface with a setter is a good idea. Shouldn't we simply inject in the loaders' constructors thecallable $choiceFilter instead?

Copy link
Contributor

@ro0NLro0NLSep 26, 2018
edited
Loading

Choose a reason for hiding this comment

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

👍 makes sense, to keep compatibility with any choice_loader we could still decorate it if the choice filter wasnt injected initially.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

I expected this question :) My goal is enable this feature for all (more and more used)CallbackChoiceLoader in current projects, automatically.

Otherwise, the feature will be enabled only for core types (e.g. Intl form types,EntityType) and will require more code and then more maintenance (injecting the$options['choice_filter'] everywhere).

This was the simplest solution I found to achieve a better DX, so callback loaders don't need to do something to enable this option.

Copy link
MemberAuthor

@ycerutoycerutoSep 26, 2018
edited
Loading

Choose a reason for hiding this comment

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

Also, this would allow for custom choice loader to enable this feature without care about injecting$options['choice_filter'] everywhere the loader is used. We only care about the implementation as a plug-and-play interface.

Btw, there are many examples of interface with a setter into the core, I'm not sure it's a problem.

Copy link
Contributor

Choose a reason for hiding this comment

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

see#28624 for a different approach

yceruto reacted with heart emoji
@ycerutoycerutoforce-pushed thechoice_filter branch 2 times, most recently from543962b tofa0a5e3CompareSeptember 26, 2018 15:45
@nicolas-grekasnicolas-grekas added this to thenext milestoneSep 29, 2018
@xabbuh
Copy link
Member

Should we close here?#28624 looks like the cleaner solution to me. Thank you@yceruto for your work on this! 👍

@yceruto
Copy link
MemberAuthor

Sure :) I need some time to review#28624 deeply, but it looks good to me.

xabbuh reacted with thumbs up emoji

@ycerutoyceruto closed thisOct 1, 2018
@ycerutoyceruto deleted the choice_filter branchOctober 27, 2018 13:54
@nicolas-grekasnicolas-grekas modified the milestones:next,4.2Nov 1, 2018
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

2 more reviewers

@ro0NLro0NLro0NL left review comments

@ogizanagiogizanagiogizanagi left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Labels

DXDX = Developer eXperience (anything that improves the experience of using Symfony)FeatureFormStatus: Needs Review

Projects

None yet

Milestone

4.2

Development

Successfully merging this pull request may close these issues.

6 participants

@yceruto@xabbuh@ro0NL@ogizanagi@nicolas-grekas@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp