Skip to content

Commit

Permalink
bug #2587 [Live] Fix default select value with preferred choices (1ed)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Kocal committed Mar 3, 2025
2 parents dade31f + 6dc6a33 commit 8a11743
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/LiveComponent/src/ComponentWithFormTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
3 changes: 3 additions & 0 deletions src/LiveComponent/tests/Unit/Form/ComponentWithFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down

0 comments on commit 8a11743

Please sign in to comment.