Contributed by
Jules Pietri
in #18332.
ChoiceType is the most powerful Symfony form type and it's used to create select drop-downs, radio buttons and checkboxes. In Symfony 3.2 we added a new feature to improve its performance: lazy loading the choice values.
First, define the choice_loader
option for the ChoiceType
and then,
use the new CallbackChoiceLoader
class to set the PHP callable executed to
get the list of choices:
1 2 3 4 5 6 7 8 | use Symfony\Component\Form\ChoiceList\Loader\CallbackChoiceLoader;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
$builder->add('constants', ChoiceType::class, [
'choice_loader' => new CallbackChoiceLoader(function() {
return StaticClass::getConstants();
},
]);
|
The CallbackChoiceLoader
class implements ChoiceLoaderInterface
, which
is now also implemented in every ChoiceType
subtype, such as CountryType
,
CurrencyType
, LanguageType
, LocaleType
and TimezoneType
.
What a Symfony developer should know about the framework: News, Jobs, Tweets, Events, Videos,...