Skip to content

Commit a1ddd9a

Browse files
Merge branch '7.4' into 8.0
* 7.4: (28 commits) [Messenger] Allow Pheanstalk v8 [TypeInfo] Fix resolving constructor type with templates fix compatibility with RelayCluster 0.12 fix type alias with template resolving fix compatibility with RelayCluster 0.11 and 0.12 [DependencyInjection] Register a custom autoloader to generate `*Config` classes when they don't exist yet [Security] Add security:oidc-token:generate command [PropertyInfo][TypeInfo] Fix resolving constructor type with templates [WebProfilerBundle] ”finish” errored requests Add support for union types on AsEventListener [Console] Update CHANGELOG to reflect attribute name changes for interactive invokable commands bump ext-redis to 6.2 and ext-relay to 0.12 minimum [TypeInfo] Fix type alias with template resolving [Console] Add support for interactive invokable commands with `#[Interact]` and `#[Ask]` attributes bump ext-relay to 0.12+ fix merge [Config] Generate the array-shape of the current node instead of the whole root node in Config classes [HttpFoundation] Deprecate Request::get() in favor of using properties ->attributes, query or request directly fix Relay Cluster 0.12 compatibility [TypeInfo] ArrayShape can resolve key type as callable instead of string ...
2 parents 8667f81 + 46b4de4 commit a1ddd9a

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

Extension/Core/Type/EnumType.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff 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;

Tests/Extension/Core/Type/EnumTypeTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff 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];

0 commit comments

Comments
 (0)