-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[Form] added the new ChoiceList
class to configure ChoiceType
options
#13182
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
Merged
javiereguiluz
merged 1 commit into
symfony:master
from
HeahDude:cached-choice_list-options
Mar 24, 2020
Merged
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
choice_loader | ||
~~~~~~~~~~~~~ | ||
|
||
**type**: :class:`Symfony\\Component\\Form\\ChoiceList\\Loader\\ChoiceLoaderInterface` | ||
|
||
The ``choice_loader`` can be used instead of ``choices`` option. It allows to | ||
create a list lazily or partially when fetching only the choices for a set of | ||
submitted values (i.e. querying a search engine like ``ElasticSearch`` can be | ||
a heavy process). | ||
|
||
You can use an instance of :class:`Symfony\\Component\\Form\\ChoiceList\\Loader\\CallbackChoiceLoader` | ||
if you want to take advantage of lazy loading:: | ||
|
||
use App\StaticClass; | ||
use Symfony\Component\Form\ChoiceList\Loader\CallbackChoiceLoader; | ||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; | ||
// ... | ||
|
||
$builder->add('loaded_choices', ChoiceType::class, [ | ||
'choice_loader' => new CallbackChoiceLoader(function() { | ||
return StaticClass::getConstants(); | ||
}), | ||
]); | ||
|
||
This will cause the call of ``StaticClass::getConstants()`` to not happen if the | ||
request is redirected and if there is no pre set or submitted data. Otherwise | ||
the choice options would need to be resolved thus triggering the callback. | ||
|
||
When you're defining a custom choice type that may be reused in many fields | ||
(like entries of a collection) or reused in multiple forms at once, you | ||
should use the :class:`Symfony\\Component\\Form\\ChoiceList\\ChoiceList` | ||
static methods to wrap the loader and make the choice list cacheable for | ||
better performance:: | ||
|
||
use App\Form\ChoiceList\CustomChoiceLoader; | ||
use App\StaticClass; | ||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\ChoiceList\ChoiceList; | ||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; | ||
use Symfony\Component\OptionsResolver\Options; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
class ConstantsType extends AbstractType | ||
{ | ||
public static function getExtendedTypes(): iterable | ||
{ | ||
return [ChoiceType::class]; | ||
} | ||
|
||
public function configureOptions(OptionsResolver $resolver) | ||
{ | ||
$resolver->setDefaults([ | ||
// the example below will create a CallbackChoiceLoader from the callable | ||
'choice_loader' => ChoiceList::lazy($this, function() { | ||
return StaticClass::getConstants(); | ||
}), | ||
|
||
// you can pass your own loader as well, depending on other options | ||
'some_key' => null, | ||
'choice_loader' => function (Options $options) { | ||
return ChoiceList::loader( | ||
// pass the instance of the type or type extension which is | ||
// currently configuring the choice list as first argument | ||
$this, | ||
// pass the other option to the loader | ||
new CustomChoiceLoader($options['some_key']), | ||
// ensure the type stores a loader per key | ||
// by using the special third argument "$vary" | ||
// an array containing anything that "changes" the loader | ||
[$options['some_key']] | ||
); | ||
}, | ||
]); | ||
} | ||
} |
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
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.