Skip to content

Commit 8a11743

Browse files
committed
bug #2587 [Live] Fix default select value with preferred choices (1ed)
This PR was merged into the 2.x branch. Discussion ---------- [Live] Fix default select value with preferred choices | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Issues | - | License | MIT When there are preferred_choices set for a choice type Symfony renders them before the actual choices so the default value in the browser will be the first value of the preferred choices. Commits ------- 6dc6a33 [Live] Fix default select value with preferred choices
2 parents dade31f + 6dc6a33 commit 8a11743

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

src/LiveComponent/src/ComponentWithFormTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ private function extractFormValues(FormView $formView): array
288288
&& !$child->vars['multiple'] // is not multiple
289289
&& !\is_string($child->vars['placeholder']) // has no placeholder (empty string is valid)
290290
) {
291-
$choices = $child->vars['choices'];
291+
$choices = $child->vars['preferred_choices'] ?: $child->vars['choices']; // preferred_choices has precedence, as they rendered before regular choices
292292
do {
293293
$choice = $choices[array_key_first($choices)];
294294
if (!$choice instanceof ChoiceGroupView) {

src/LiveComponent/tests/Fixtures/Form/FormWithManyDifferentFieldsType.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,38 @@ public function buildForm(FormBuilderInterface $builder, array $options)
7474
'foo' => 1,
7575
],
7676
])
77+
->add('choice_required_with_preferred_choices_array', ChoiceType::class, [
78+
'choices' => [
79+
'Bar Group' => [
80+
'Bar Label' => 'ok',
81+
'Foo Label' => 'foo_value',
82+
],
83+
'foo' => 1,
84+
],
85+
'preferred_choices' => ['foo_value'],
86+
])
87+
->add('choice_required_with_preferred_choices_callback', ChoiceType::class, [
88+
'choices' => [
89+
'Bar Group' => [
90+
'Bar Label' => 'ok',
91+
'Foo Label' => 'foo_value',
92+
],
93+
'foo' => 1,
94+
],
95+
'preferred_choices' => function ($choice): bool {
96+
return is_int($choice);
97+
},
98+
])
99+
->add('choice_required_with_empty_preferred_choices', ChoiceType::class, [
100+
'choices' => [
101+
'Bar Group' => [
102+
'Bar Label' => 'ok',
103+
'Foo Label' => 'foo_value',
104+
],
105+
'foo' => 1,
106+
],
107+
'preferred_choices' => [],
108+
])
77109
->add('choice_expanded', ChoiceType::class, [
78110
'choices' => [
79111
'foo' => 1,

src/LiveComponent/tests/Functional/Form/ComponentWithFormTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ public function testHandleCheckboxChanges(): void
182182
'choice_required_with_empty_placeholder' => '',
183183
'choice_required_without_placeholder' => '2',
184184
'choice_required_without_placeholder_and_choice_group' => 'ok',
185+
'choice_required_with_preferred_choices_array' => 'foo_value',
186+
'choice_required_with_preferred_choices_callback' => '1',
187+
'choice_required_with_empty_preferred_choices' => 'ok',
185188
'choice_expanded' => '',
186189
'choice_multiple' => ['2'],
187190
'select_multiple' => ['2'],

src/LiveComponent/tests/Unit/Form/ComponentWithFormTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ public function testFormValues(): void
4949
'choice_required_with_empty_placeholder' => '',
5050
'choice_required_without_placeholder' => '2',
5151
'choice_required_without_placeholder_and_choice_group' => 'ok',
52+
'choice_required_with_preferred_choices_array' => 'foo_value',
53+
'choice_required_with_preferred_choices_callback' => '1',
54+
'choice_required_with_empty_preferred_choices' => 'ok',
5255
'choice_expanded' => '',
5356
'choice_multiple' => ['2'],
5457
'select_multiple' => ['2'],

0 commit comments

Comments
 (0)