Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LiveComponent] fix required select not initialized #2403

Open
wants to merge 4 commits into
base: 2.x
Choose a base branch
from

Conversation

dsoriano
Copy link
Contributor

Q A
Bug fix? yes
New feature? no
Issues Fix #1332
License MIT

Initialize required select(ChoiceType or EntityType) with the first entry. Not applied to multiple or expanded.

@carsonbot carsonbot added Bug Bug Fix Status: Needs Review Needs to be reviewed labels Nov 22, 2024
@dsoriano dsoriano changed the title #1332 fix required select not initialized [LiveComponent] #1332 fix required select not initialized Nov 22, 2024
@dsoriano
Copy link
Contributor Author

If someone knows why ux.symfony.com tests failed... it's gives me headache.. Nothing was changed in the ux.symfony.com directory and in local the tests are ok.

@smnandre
Copy link
Member

It appears this PR changes are indeed responsible...

Capture d’écran 2024-11-22 à 17 49 54

The ux.symfony.com tests on the CI use the UX packages of the PR...

Locally to reproduce you should go into the ux.symfony.com repository and then call the link locally command

cd ux.symfony.com
php bin/link-locally

Then refresh

composer update
php bin/console c:c
symfony server:start -d

@smnandre smnandre added the Status: Needs Work Additional work is needed label Nov 22, 2024
@dsoriano
Copy link
Contributor Author

ok great, thank you ! I will take a look on it.
Weird that locally the tests for ux.symfony.com are ok, but I probably missed something...

Thanks again

@smnandre
Copy link
Member

You did not run the php bin/link-locally i suppose ?

I let you look at the suggestion, i found the source of the problem.

I let you check locally and tell me if that seems ok for you ?

Comment on lines 261 to 275
continue;
}

if (\array_key_exists('choices', $child->vars)
&& $child->vars['required']
&& !$child->vars['multiple']
&& !$child->vars['expanded']
&& null === ($values[$name] ?? null)
) {
if (0 === \count($choices = $child->vars['choices'])) {
throw new UnprocessableEntityHttpException(\sprintf('The required field "%s" is missing and has no default value', $name));
}

$values[$name] = reset($choices)->value;
} else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
continue;
}
if (\array_key_exists('choices', $child->vars)
&& $child->vars['required']
&& !$child->vars['multiple']
&& !$child->vars['expanded']
&& null === ($values[$name] ?? null)
) {
if (0 === \count($choices = $child->vars['choices'])) {
throw new UnprocessableEntityHttpException(\sprintf('The required field "%s" is missing and has no default value', $name));
}
$values[$name] = reset($choices)->value;
} else {
continue;
}
// Special handling: required <select> fields with no value, no
// placeholder and no multiple attribute should have their value set
// to the first choice, if it exists.
if (
\array_key_exists('choices', $child->vars)
&& $child->vars['required']
&& !$child->vars['disabled']
&& !$child->vars['value']
&& !$child->vars['placeholder']
&& !$child->vars['multiple']
&& !$child->vars['expanded']
&& $child->vars['choices']
) {
if (null !== $firstKey = array_key_first($child->vars['choices'])) {
$values[$name] = $child->vars['choices'][$firstKey]->value ?? null;
continue;
}
}

There were some pre-condition checks missing. This one seems more robust, i'll let you check.

Comment on lines 46 to 54
'required' => false,
])
->add('choice_required', ChoiceType::class, [
'choices' => [
'foo' => 1,
'bar' => 2,
],
])
->add('choice_expanded', ChoiceType::class, [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'required' => false,
])
->add('choice_required', ChoiceType::class, [
'choices' => [
'foo' => 1,
'bar' => 2,
],
])
->add('choice_expanded', ChoiceType::class, [
'required' => false,
])
->add('choice_required_with_placeholder', ChoiceType::class, [
'choices' => [
'bar' => 2,
'foo' => 1,
],
'required' => true,
'placeholder' => 'foo',
])
->add('choice_required_without_placeholder', ChoiceType::class, [
'choices' => [
'bar' => 2,
'foo' => 1,
],
'required' => true,
'placeholder' => false,
])
->add('choice_expanded', ChoiceType::class, [

I added a test to distinct the two scenarios

Comment on lines 181 to 185
'choice_required' => '1',
'choice_expanded' => '',
'choice_multiple' => ['2'],
'select_multiple' => ['2'],
'entity' => (string) $id,
Copy link
Member

@smnandre smnandre Nov 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'choice_required' => '1',
'choice_expanded' => '',
'choice_multiple' => ['2'],
'select_multiple' => ['2'],
'entity' => (string) $id,
'choice_required_with_placeholder' => '',
'choice_required_without_placeholder' => '2',
'choice_expanded' => '',
'choice_multiple' => ['2'],
'select_multiple' => ['2'],
'entity' => (string) $id,

@smnandre smnandre added Status: Waiting Feedback Needs feedback from the author and removed Status: Needs Review Needs to be reviewed Status: Needs Work Additional work is needed labels Nov 22, 2024
@dsoriano
Copy link
Contributor Author

@smnandre indeed I missed the link-locally, thanks for your help. Suggestions was good, all is ok now. Many thanks !

Copy link
Member

@smnandre smnandre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much @dsoriano!

@carsonbot carsonbot added the Status: Reviewed Has been reviewed by a maintainer label Nov 23, 2024
@smnandre smnandre added Feature New Feature and removed Status: Waiting Feedback Needs feedback from the author labels Nov 23, 2024
@smnandre smnandre changed the title [LiveComponent] #1332 fix required select not initialized [LiveComponent] fix required select not initialized Nov 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Bug Fix Feature New Feature Status: Reviewed Has been reviewed by a maintainer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[UX] Live component - form select required not initialized
3 participants