|
| 1 | +choice_loader |
| 2 | +~~~~~~~~~~~~~ |
| 3 | + |
| 4 | +**type**: :class:`Symfony\\Component\\Form\\ChoiceList\\Loader\\ChoiceLoaderInterface` |
| 5 | + |
| 6 | +The ``choice_loader`` can be used instead of ``choices`` option. It allows to |
| 7 | +create a list lazily or partially when fetching only the choices for a set of |
| 8 | +submitted values (i.e. querying a search engine like ``ElasticSearch`` can be |
| 9 | +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 | + |
| 25 | +This will cause the call of ``StaticClass::getConstants()`` to not happen if the |
| 26 | +request is redirected and if there is no pre set or submitted data. Otherwise |
| 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