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 @@ -267,6 +267,43 @@ public function testChoiceLabelTranslatable()
267267 $ this ->assertEquals ('Left ' , $ view ->children [0 ]->vars ['label ' ]->trans (new IdentityTranslator ()));
268268 }
269269
270+ public function testChoices ()
271+ {
272+ $ form = $ this ->factory ->create ($ this ->getTestedType (), null , [
273+ 'multiple ' => false ,
274+ 'expanded ' => true ,
275+ 'class ' => Answer::class,
276+ 'choices ' => [
277+ Answer::Yes,
278+ Answer::No,
279+ ],
280+ ]);
281+
282+ $ view = $ form ->createView ();
283+
284+ $ this ->assertCount (2 , $ view ->children );
285+ $ this ->assertSame ('Yes ' , $ view ->children [0 ]->vars ['label ' ]);
286+ $ this ->assertSame ('No ' , $ view ->children [1 ]->vars ['label ' ]);
287+ }
288+
289+ public function testChoicesWithLabels ()
290+ {
291+ $ form = $ this ->factory ->create ($ this ->getTestedType (), null , [
292+ 'multiple ' => false ,
293+ 'expanded ' => true ,
294+ 'class ' => Answer::class,
295+ 'choices ' => [
296+ 'yes ' => Answer::Yes,
297+ 'no ' => Answer::No,
298+ ],
299+ ]);
300+
301+ $ view = $ form ->createView ();
302+
303+ $ this ->assertSame ('yes ' , $ view ->children [0 ]->vars ['label ' ]);
304+ $ this ->assertSame ('no ' , $ view ->children [1 ]->vars ['label ' ]);
305+ }
306+
270307 protected function getTestOptions (): array
271308 {
272309 return ['class ' => Suit::class];
You can’t perform that action at this time.
0 commit comments