Today we are going to tackle a problem that I’ve recently faced with Symfony form types. Scenario: I need a choice type of a certain entity (Foo) with values that follows a certain logic
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
<?php class FooChoiceType extends AbstractType { private $fooAdapter; public function __construct(FooAdapter $fooAdapter) { $this->fooAdapter = $fooAdapter; } function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults([ 'required' => true, 'multiple' => false, 'choices' => $this->fooAdapter->getFoos(), ]); } public function getParent() { return ChoiceType::class; } } |
I’ve built up a FooChoiceType because…