Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading