Skip to content

Commit 080d402

Browse files
committed
[Live] Fix default select value with preferred choices
1 parent 5487937 commit 080d402

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

src/LiveComponent/src/ComponentWithFormTrait.php

+1-1
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'];
292292
do {
293293
$choice = $choices[array_key_first($choices)];
294294
if (!$choice instanceof ChoiceGroupView) {

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

+22
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,28 @@ 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+
])
7799
->add('choice_expanded', ChoiceType::class, [
78100
'choices' => [
79101
'foo' => 1,

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

+2
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ 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',
185187
'choice_expanded' => '',
186188
'choice_multiple' => ['2'],
187189
'select_multiple' => ['2'],

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

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ 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',
5254
'choice_expanded' => '',
5355
'choice_multiple' => ['2'],
5456
'select_multiple' => ['2'],

0 commit comments

Comments
 (0)