|
1 |
| -choice_loader ~~~~~~~~~~~~~ **type**: |
2 |
| -:class:`Symfony\\Component\\Form\\ChoiceList\\Loader\\ChoiceLoaderInterface` The |
3 |
| -``choice_loader`` option can be used instead of the ``choices`` option. It |
| 1 | +choice_loader |
| 2 | +~~~~~~~~~~~~~ |
| 3 | + |
| 4 | +**type**: :class:`Symfony\\Component\\Form\\ChoiceList\\Loader\\ChoiceLoaderInterface` |
| 5 | + |
| 6 | +The ``choice_loader`` option can be used instead of the ``choices`` option. It |
4 | 7 | allows to create a list lazily or partially when fetching only the choices for a
|
5 | 8 | set of submitted values (i.e. querying a search engine like ``ElasticSearch``
|
6 |
| -can be a heavy process). You can use an instance of |
7 |
| -:class:`Symfony\\Component\\Form\\ChoiceList\\Loader\\CallbackChoiceLoader` if |
8 |
| -you want to take advantage of lazy loading:: use App\StaticClass; use |
9 |
| -Symfony\Component\Form\ChoiceList\Loader\CallbackChoiceLoader; use |
10 |
| -Symfony\Component\Form\Extension\Core\Type\ChoiceType; // ... |
11 |
| -$builder->add('loaded_choices', ChoiceType::class, [ 'choice_loader' => new |
12 |
| -CallbackChoiceLoader(function() { return StaticClass::getConstants(); }), ]); |
| 9 | +can be a heavy process). |
| 10 | + |
| 11 | +You can use an instance of :class:`Symfony\\Component\\Form\\ChoiceList\\Loader\\CallbackChoiceLoader` |
| 12 | +if you want to take advantage of lazy loading:: |
| 13 | + |
| 14 | + use App\StaticClass; |
| 15 | + use Symfony\Component\Form\ChoiceList\Loader\CallbackChoiceLoader; |
| 16 | + use Symfony\Component\Form\Extension\Core\Type\ChoiceType; |
| 17 | + // ... |
| 18 | + |
| 19 | + $builder->add('loaded_choices', ChoiceType::class, [ |
| 20 | + 'choice_loader' => new CallbackChoiceLoader(function() { |
| 21 | + return StaticClass::getConstants(); |
| 22 | + }), |
| 23 | + ]); |
| 24 | + |
13 | 25 | This will cause the call of ``StaticClass::getConstants()`` to not happen if the
|
14 | 26 | request is redirected and if there is no pre set or submitted data. Otherwise
|
15 |
| -the choice options would need to be resolved thus triggering the callback. When |
16 |
| -you're defining a custom choice type that may be reused in many fields (like |
17 |
| -entries of a collection) or reused in multiple forms at once, you should use the |
18 |
| -:class:`Symfony\\Component\\Form\\ChoiceList\\ChoiceList` static methods to wrap |
19 |
| -the loader and make the choice list cacheable for better performance:: // |
20 |
| -src/Form/ConstantsType.php namespace App\Form; use |
21 |
| -App\Form\ChoiceList\CustomChoiceLoader; use App\StaticClass; use |
22 |
| -Symfony\Component\Form\AbstractType; use |
23 |
| -Symfony\Component\Form\ChoiceList\ChoiceList; use |
24 |
| -Symfony\Component\Form\Extension\Core\Type\ChoiceType; use |
25 |
| -Symfony\Component\OptionsResolver\Options; use |
26 |
| -Symfony\Component\OptionsResolver\OptionsResolver; class ConstantsType extends |
27 |
| -AbstractType { public static function getExtendedTypes(): iterable { return |
28 |
| -[ChoiceType::class]; } public function configureOptions(OptionsResolver |
29 |
| -$resolver) { $resolver->setDefaults([ // the example below will create a |
30 |
| -CallbackChoiceLoader from the callable 'choice_loader' => |
31 |
| -ChoiceList::lazy($this, function() { return StaticClass::getConstants(); }), // |
32 |
| -you can pass your own loader as well, depending on other options 'some_key' => |
33 |
| -null, 'choice_loader' => function (Options $options) { return |
34 |
| -ChoiceList::loader( // pass the instance of the type or type extension which is |
35 |
| -// currently configuring the choice list as first argument $this, // pass the |
36 |
| -other option to the loader new CustomChoiceLoader($options['some_key']), // |
37 |
| -ensure the type stores a loader per key // by using the special third argument |
38 |
| -"$vary" // an array containing anything that "changes" the loader |
39 |
| -[$options['some_key']] ); }, ]); } } |
| 27 | +the choice options would need to be resolved thus triggering the callback. |
| 28 | + |
| 29 | +When you're defining a custom choice type that may be reused in many fields |
| 30 | +(like entries of a collection) or reused in multiple forms at once, you |
| 31 | +should use the :class:`Symfony\\Component\\Form\\ChoiceList\\ChoiceList` |
| 32 | +static methods to wrap the loader and make the choice list cacheable for |
| 33 | +better performance:: |
| 34 | + |
| 35 | + use App\Form\ChoiceList\CustomChoiceLoader; |
| 36 | + use App\StaticClass; |
| 37 | + use Symfony\Component\Form\AbstractType; |
| 38 | + use Symfony\Component\Form\ChoiceList\ChoiceList; |
| 39 | + use Symfony\Component\Form\Extension\Core\Type\ChoiceType; |
| 40 | + use Symfony\Component\OptionsResolver\Options; |
| 41 | + use Symfony\Component\OptionsResolver\OptionsResolver; |
| 42 | + |
| 43 | + class ConstantsType extends AbstractType |
| 44 | + { |
| 45 | + public static function getExtendedTypes(): iterable |
| 46 | + { |
| 47 | + return [ChoiceType::class]; |
| 48 | + } |
| 49 | + |
| 50 | + public function configureOptions(OptionsResolver $resolver) |
| 51 | + { |
| 52 | + $resolver->setDefaults([ |
| 53 | + // the example below will create a CallbackChoiceLoader from the callable |
| 54 | + 'choice_loader' => ChoiceList::lazy($this, function() { |
| 55 | + return StaticClass::getConstants(); |
| 56 | + }), |
| 57 | + |
| 58 | + // you can pass your own loader as well, depending on other options |
| 59 | + 'some_key' => null, |
| 60 | + 'choice_loader' => function (Options $options) { |
| 61 | + return ChoiceList::loader( |
| 62 | + // pass the instance of the type or type extension which is |
| 63 | + // currently configuring the choice list as first argument |
| 64 | + $this, |
| 65 | + // pass the other option to the loader |
| 66 | + new CustomChoiceLoader($options['some_key']), |
| 67 | + // ensure the type stores a loader per key |
| 68 | + // by using the special third argument "$vary" |
| 69 | + // an array containing anything that "changes" the loader |
| 70 | + [$options['some_key']] |
| 71 | + ); |
| 72 | + }, |
| 73 | + ]); |
| 74 | + } |
| 75 | + } |
0 commit comments