Skip to content

Commit 6008eed

Browse files
committed
bug #2426 [LiveComponent] Handle loose comparison with empty placeholder (Matthieu Renard)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- [LiveComponent] Handle loose comparison with empty placeholder Fix for a bug that arises when using an empty placeholder in a required select field, that is wrongly treated as an absence of placeholder by LiveComponents. | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Issues | Fix #2425 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT LiveComponents test placeholders of required selects to detect when to set a default value from the choices (i.e. if the select is required and does not have a placeholder, among other conditions) by just casting the placeholder to a boolean. This leads to empty placeholders (placeholders that are the empty string) to be treated as an absence of placeholders when they should not. This MR fixes this by replacing the condition on placeholders with a more appropriate one. According to the docs https://symfony.com/doc/current/reference/forms/types/choice.html#placeholder and https://symfony.com/doc/current/reference/forms/types/choice.html#field-variables, the placeholder can be a boolean (`false` indicates that there should not be a placeholder), a string (the text to display for the empty value) or a TranslatableMessage (same as string, but goes through a translator before being displayed). The vars property is either the value of the field, or `null` if there is no placeholder specified. From this, I considered that there is no placeholder either if the placeholder holds `false` or `null`. I added a simple test that checks the behaviour when there is a required select with an empty placeholder ; we expect the value of the field to be empty in such cases when we do not modify it. Commits ------- 3965302 [LiveComponent] Handle loose comparison with empty placeholder
2 parents 49449f2 + 3965302 commit 6008eed

File tree

4 files changed

+10
-1
lines changed

4 files changed

+10
-1
lines changed

src/LiveComponent/src/ComponentWithFormTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ private function extractFormValues(FormView $formView): array
265265
&& $child->vars['required']
266266
&& !$child->vars['disabled']
267267
&& !$child->vars['value']
268-
&& !$child->vars['placeholder']
268+
&& (false === $child->vars['placeholder'] || null === $child->vars['placeholder'])
269269
&& !$child->vars['multiple']
270270
&& !$child->vars['expanded']
271271
&& $child->vars['choices']

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ public function buildForm(FormBuilderInterface $builder, array $options)
5252
],
5353
'placeholder' => 'foo',
5454
])
55+
->add('choice_required_with_empty_placeholder', ChoiceType::class, [
56+
'choices' => [
57+
'bar' => 2,
58+
'foo' => 1,
59+
],
60+
'placeholder' => '',
61+
])
5562
->add('choice_required_without_placeholder', ChoiceType::class, [
5663
'choices' => [
5764
'bar' => 2,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ public function testHandleCheckboxChanges(): void
179179
'range' => '',
180180
'choice' => '',
181181
'choice_required_with_placeholder' => '',
182+
'choice_required_with_empty_placeholder' => '',
182183
'choice_required_without_placeholder' => '2',
183184
'choice_expanded' => '',
184185
'choice_multiple' => ['2'],

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function testFormValues(): void
4444
'range' => '',
4545
'choice' => '',
4646
'choice_required_with_placeholder' => '',
47+
'choice_required_with_empty_placeholder' => '',
4748
'choice_required_without_placeholder' => '2',
4849
'choice_expanded' => '',
4950
'choice_multiple' => ['2'],

0 commit comments

Comments
 (0)