From 39653024daf4a30a696987259ece8805eefafda5 Mon Sep 17 00:00:00 2001 From: Matthieu Renard Date: Thu, 5 Dec 2024 10:28:26 +0100 Subject: [PATCH] [LiveComponent] Handle loose comparison with empty placeholder --- src/LiveComponent/src/ComponentWithFormTrait.php | 2 +- .../Fixtures/Form/FormWithManyDifferentFieldsType.php | 7 +++++++ .../tests/Functional/Form/ComponentWithFormTest.php | 1 + .../tests/Unit/Form/ComponentWithFormTest.php | 1 + 4 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/LiveComponent/src/ComponentWithFormTrait.php b/src/LiveComponent/src/ComponentWithFormTrait.php index 0e088e02239..c8ab525d495 100644 --- a/src/LiveComponent/src/ComponentWithFormTrait.php +++ b/src/LiveComponent/src/ComponentWithFormTrait.php @@ -265,7 +265,7 @@ private function extractFormValues(FormView $formView): array && $child->vars['required'] && !$child->vars['disabled'] && !$child->vars['value'] - && !$child->vars['placeholder'] + && (false === $child->vars['placeholder'] || null === $child->vars['placeholder']) && !$child->vars['multiple'] && !$child->vars['expanded'] && $child->vars['choices'] diff --git a/src/LiveComponent/tests/Fixtures/Form/FormWithManyDifferentFieldsType.php b/src/LiveComponent/tests/Fixtures/Form/FormWithManyDifferentFieldsType.php index 0524f957a10..f2a0e322104 100644 --- a/src/LiveComponent/tests/Fixtures/Form/FormWithManyDifferentFieldsType.php +++ b/src/LiveComponent/tests/Fixtures/Form/FormWithManyDifferentFieldsType.php @@ -52,6 +52,13 @@ public function buildForm(FormBuilderInterface $builder, array $options) ], 'placeholder' => 'foo', ]) + ->add('choice_required_with_empty_placeholder', ChoiceType::class, [ + 'choices' => [ + 'bar' => 2, + 'foo' => 1, + ], + 'placeholder' => '', + ]) ->add('choice_required_without_placeholder', ChoiceType::class, [ 'choices' => [ 'bar' => 2, diff --git a/src/LiveComponent/tests/Functional/Form/ComponentWithFormTest.php b/src/LiveComponent/tests/Functional/Form/ComponentWithFormTest.php index f13be55116e..a3ed0f7c5e4 100644 --- a/src/LiveComponent/tests/Functional/Form/ComponentWithFormTest.php +++ b/src/LiveComponent/tests/Functional/Form/ComponentWithFormTest.php @@ -179,6 +179,7 @@ public function testHandleCheckboxChanges(): void 'range' => '', 'choice' => '', 'choice_required_with_placeholder' => '', + 'choice_required_with_empty_placeholder' => '', 'choice_required_without_placeholder' => '2', 'choice_expanded' => '', 'choice_multiple' => ['2'], diff --git a/src/LiveComponent/tests/Unit/Form/ComponentWithFormTest.php b/src/LiveComponent/tests/Unit/Form/ComponentWithFormTest.php index 2cf89e3e43c..931430d8b0a 100644 --- a/src/LiveComponent/tests/Unit/Form/ComponentWithFormTest.php +++ b/src/LiveComponent/tests/Unit/Form/ComponentWithFormTest.php @@ -44,6 +44,7 @@ public function testFormValues(): void 'range' => '', 'choice' => '', 'choice_required_with_placeholder' => '', + 'choice_required_with_empty_placeholder' => '', 'choice_required_without_placeholder' => '2', 'choice_expanded' => '', 'choice_multiple' => ['2'],