File tree Expand file tree Collapse file tree 2 files changed +44
-1
lines changed
Tests/Extension/Core/Type Expand file tree Collapse file tree 2 files changed +44
-1
lines changed Original file line number Diff line number Diff line change @@ -30,7 +30,13 @@ public function configureOptions(OptionsResolver $resolver): void
3030 ->setAllowedTypes ('class ' , 'string ' )
3131 ->setAllowedValues ('class ' , enum_exists (...))
3232 ->setDefault ('choices ' , static fn (Options $ options ): array => $ options ['class ' ]::cases ())
33- ->setDefault ('choice_label ' , static fn (\UnitEnum $ choice ) => $ choice instanceof TranslatableInterface ? $ choice : $ choice ->name )
33+ ->setDefault ('choice_label ' , static function (Options $ options ) {
34+ if (\is_array ($ options ['choices ' ]) && !array_is_list ($ options ['choices ' ])) {
35+ return null ;
36+ }
37+
38+ return static fn (\UnitEnum $ choice ) => $ choice instanceof TranslatableInterface ? $ choice : $ choice ->name ;
39+ })
3440 ->setDefault ('choice_value ' , static function (Options $ options ): ?\Closure {
3541 if (!is_a ($ options ['class ' ], \BackedEnum::class, true )) {
3642 return null ;
Original file line number Diff line number Diff line change @@ -274,6 +274,43 @@ public function testChoiceLabelTranslatable()
274274 $ this ->assertEquals ('Left ' , $ view ->children [0 ]->vars ['label ' ]->trans (new IdentityTranslator ()));
275275 }
276276
277+ public function testChoices ()
278+ {
279+ $ form = $ this ->factory ->create ($ this ->getTestedType (), null , [
280+ 'multiple ' => false ,
281+ 'expanded ' => true ,
282+ 'class ' => Answer::class,
283+ 'choices ' => [
284+ Answer::Yes,
285+ Answer::No,
286+ ],
287+ ]);
288+
289+ $ view = $ form ->createView ();
290+
291+ $ this ->assertCount (2 , $ view ->children );
292+ $ this ->assertSame ('Yes ' , $ view ->children [0 ]->vars ['label ' ]);
293+ $ this ->assertSame ('No ' , $ view ->children [1 ]->vars ['label ' ]);
294+ }
295+
296+ public function testChoicesWithLabels ()
297+ {
298+ $ form = $ this ->factory ->create ($ this ->getTestedType (), null , [
299+ 'multiple ' => false ,
300+ 'expanded ' => true ,
301+ 'class ' => Answer::class,
302+ 'choices ' => [
303+ 'yes ' => Answer::Yes,
304+ 'no ' => Answer::No,
305+ ],
306+ ]);
307+
308+ $ view = $ form ->createView ();
309+
310+ $ this ->assertSame ('yes ' , $ view ->children [0 ]->vars ['label ' ]);
311+ $ this ->assertSame ('no ' , $ view ->children [1 ]->vars ['label ' ]);
312+ }
313+
277314 protected function getTestOptions (): array
278315 {
279316 return ['class ' => Suit::class];
You can’t perform that action at this time.
0 commit comments