|
| 1 | +``choice_filter`` |
| 2 | +~~~~~~~~~~~~~~~~~ |
| 3 | + |
| 4 | +**type**: ``callable``, ``string`` or :class:`Symfony\\Component\\PropertyAccess\\PropertyPath` **default**: ``null`` |
| 5 | + |
| 6 | +.. versionadded:: 5.1 |
| 7 | + |
| 8 | + The ``choice_filter`` option has been introduced in Symfony 5.1. |
| 9 | + |
| 10 | +When using predefined choice types from Symfony core or vendor libraries (i.e. |
| 11 | +:doc:`CountryType </reference/forms/types/country>`) this option let you define |
| 12 | +a callable that take each choice as only argument and must return ``true`` to |
| 13 | +keep it or ``false`` to discard it:: |
| 14 | + |
| 15 | + // src/Form/Type/AddressType.php |
| 16 | + namespace App\Form\Type; |
| 17 | + |
| 18 | + use Symfony\Component\Form\AbstractType; |
| 19 | + use Symfony\Component\Form\Extension\Core\Type\CountryType; |
| 20 | + use Symfony\Component\Form\FormBuilderInterface; |
| 21 | + use Symfony\Component\OptionsResolver\OptionsResolver; |
| 22 | + |
| 23 | + class AddressType extends AbstractType |
| 24 | + { |
| 25 | + public function configureOptions(OptionsResolver $resolver) |
| 26 | + { |
| 27 | + $resolver |
| 28 | + ->setDefaults([ |
| 29 | + // enable this type to accept a limited set of countries |
| 30 | + 'allowed_countries' => null, |
| 31 | + ]) |
| 32 | + ; |
| 33 | + } |
| 34 | + |
| 35 | + public function buildForm(FormBuilderInterface $builder, array $options) |
| 36 | + { |
| 37 | + $allowedCountries = $options['allowed_countries']; |
| 38 | + |
| 39 | + $builder |
| 40 | + // ... |
| 41 | + ->add('country', CountryType::class, [ |
| 42 | + // if the AddressType "allowed_countries" option is passed, |
| 43 | + // use it to create a filter |
| 44 | + 'choice_filter' => $allowedCountries ? function ($countryCode) use ($allowedCountries) { |
| 45 | + return in_array($countryCode, $allowedCountries, true); |
| 46 | + } : null, |
| 47 | + |
| 48 | + ]) |
| 49 | + ; |
| 50 | + } |
| 51 | + |
| 52 | +The option can be a callable or a property path when choices are objects:: |
| 53 | + |
| 54 | + // ... |
| 55 | + $builder |
| 56 | + ->add('category', ChoiceType::class, [ |
| 57 | + // ... |
| 58 | + 'choice_filter' => 'isSelectable', |
| 59 | + ]) |
| 60 | + ; |
| 61 | + |
| 62 | +.. tip:: |
| 63 | + |
| 64 | + Considering this ``AddressType`` could be an entry of a ``CollectionType`` |
| 65 | + you should use the :class:`Symfony\\Component\\Form\\ChoiceList\\ChoiceList` |
| 66 | + class helper to enable caching:: |
| 67 | + |
| 68 | + use Symfony\Component\Form\ChoiceList\ChoiceList; |
| 69 | + |
| 70 | + // src/Form/Type/AddressType.php |
| 71 | + // ... |
| 72 | + 'choice_filter' => $allowedCountries ? ChoiceList::filter( |
| 73 | + // pass the type as first argument |
| 74 | + $this, |
| 75 | + function ($countryCode) use ($allowedCountries) { |
| 76 | + return in_array($countryCode, $allowedCountries, true); |
| 77 | + }, |
| 78 | + // pass the option that makes the filter "vary" to compute a unique hash |
| 79 | + $allowedCountries |
| 80 | + ) : null, |
| 81 | + |
| 82 | + See the :ref:`"choice_loader" option documentation <reference-form-choice-loader>`. |
0 commit comments