From 6dc6a338a82fcbfb4fd9f614cce0029a0e6cac26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Egyed?= Date: Fri, 21 Feb 2025 19:38:16 +0100 Subject: [PATCH] [Live] Fix default select value with preferred choices --- .../src/ComponentWithFormTrait.php | 2 +- .../Form/FormWithManyDifferentFieldsType.php | 32 +++++++++++++++++++ .../Functional/Form/ComponentWithFormTest.php | 3 ++ .../tests/Unit/Form/ComponentWithFormTest.php | 3 ++ 4 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/LiveComponent/src/ComponentWithFormTrait.php b/src/LiveComponent/src/ComponentWithFormTrait.php index a25a2d1dc02..00c98419263 100644 --- a/src/LiveComponent/src/ComponentWithFormTrait.php +++ b/src/LiveComponent/src/ComponentWithFormTrait.php @@ -288,7 +288,7 @@ private function extractFormValues(FormView $formView): array && !$child->vars['multiple'] // is not multiple && !\is_string($child->vars['placeholder']) // has no placeholder (empty string is valid) ) { - $choices = $child->vars['choices']; + $choices = $child->vars['preferred_choices'] ?: $child->vars['choices']; // preferred_choices has precedence, as they rendered before regular choices do { $choice = $choices[array_key_first($choices)]; if (!$choice instanceof ChoiceGroupView) { diff --git a/src/LiveComponent/tests/Fixtures/Form/FormWithManyDifferentFieldsType.php b/src/LiveComponent/tests/Fixtures/Form/FormWithManyDifferentFieldsType.php index 59537991d57..bc3794a22c8 100644 --- a/src/LiveComponent/tests/Fixtures/Form/FormWithManyDifferentFieldsType.php +++ b/src/LiveComponent/tests/Fixtures/Form/FormWithManyDifferentFieldsType.php @@ -74,6 +74,38 @@ public function buildForm(FormBuilderInterface $builder, array $options) 'foo' => 1, ], ]) + ->add('choice_required_with_preferred_choices_array', ChoiceType::class, [ + 'choices' => [ + 'Bar Group' => [ + 'Bar Label' => 'ok', + 'Foo Label' => 'foo_value', + ], + 'foo' => 1, + ], + 'preferred_choices' => ['foo_value'], + ]) + ->add('choice_required_with_preferred_choices_callback', ChoiceType::class, [ + 'choices' => [ + 'Bar Group' => [ + 'Bar Label' => 'ok', + 'Foo Label' => 'foo_value', + ], + 'foo' => 1, + ], + 'preferred_choices' => function ($choice): bool { + return is_int($choice); + }, + ]) + ->add('choice_required_with_empty_preferred_choices', ChoiceType::class, [ + 'choices' => [ + 'Bar Group' => [ + 'Bar Label' => 'ok', + 'Foo Label' => 'foo_value', + ], + 'foo' => 1, + ], + 'preferred_choices' => [], + ]) ->add('choice_expanded', ChoiceType::class, [ 'choices' => [ 'foo' => 1, diff --git a/src/LiveComponent/tests/Functional/Form/ComponentWithFormTest.php b/src/LiveComponent/tests/Functional/Form/ComponentWithFormTest.php index bfbe477c948..e0d72870b8e 100644 --- a/src/LiveComponent/tests/Functional/Form/ComponentWithFormTest.php +++ b/src/LiveComponent/tests/Functional/Form/ComponentWithFormTest.php @@ -182,6 +182,9 @@ public function testHandleCheckboxChanges(): void 'choice_required_with_empty_placeholder' => '', 'choice_required_without_placeholder' => '2', 'choice_required_without_placeholder_and_choice_group' => 'ok', + 'choice_required_with_preferred_choices_array' => 'foo_value', + 'choice_required_with_preferred_choices_callback' => '1', + 'choice_required_with_empty_preferred_choices' => 'ok', 'choice_expanded' => '', 'choice_multiple' => ['2'], 'select_multiple' => ['2'], diff --git a/src/LiveComponent/tests/Unit/Form/ComponentWithFormTest.php b/src/LiveComponent/tests/Unit/Form/ComponentWithFormTest.php index 01941f10656..93dcaa0e852 100644 --- a/src/LiveComponent/tests/Unit/Form/ComponentWithFormTest.php +++ b/src/LiveComponent/tests/Unit/Form/ComponentWithFormTest.php @@ -49,6 +49,9 @@ public function testFormValues(): void 'choice_required_with_empty_placeholder' => '', 'choice_required_without_placeholder' => '2', 'choice_required_without_placeholder_and_choice_group' => 'ok', + 'choice_required_with_preferred_choices_array' => 'foo_value', + 'choice_required_with_preferred_choices_callback' => '1', + 'choice_required_with_empty_preferred_choices' => 'ok', 'choice_expanded' => '', 'choice_multiple' => ['2'], 'select_multiple' => ['2'],