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

Merged
merged 1 commit into from
Nov 29, 2024

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 force-pushed the #1332-fix-required-select branch from dfbcd61 to 6a24416 Compare November 22, 2024 10:53
@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 61
'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 186
'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
@Kocal Kocal force-pushed the #1332-fix-required-select branch from 891bf54 to 9d1dada Compare November 29, 2024 15:31
@Kocal
Copy link
Member

Kocal commented Nov 29, 2024

Thank you Denis and Simon :)

@Kocal Kocal merged commit fd7609f into symfony:2.x Nov 29, 2024
24 checks passed
smnandre added a commit that referenced this pull request Jan 25, 2025
…ues()` with edge cases (smnandre)

This PR was squashed before being merged into the 2.x branch.

Discussion
----------

[LiveComponent] Fix `ComponentWithFormTrait::extractFormValues()` with edge cases

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Issues        | Fix #2487
| License       | MIT

#2403 fixed an old bug to simulate browser behaviour when a select field has no option selected,  no placeholder, and is required (it then uses the first option)
#2425 and #2426 fixed the way we handled empty strings as placeholders

In #2487 `@maciazek` explained us how a custom type with a "choices" option became unusable since the last changes.

Also.. while I was reading all this I realized we returned wrong values here when "option groups" were used.. leading to other problems.

I updated the code of `extractFormValues()` method to:
* check if most of the caracteristic keys added in `ChoiceType::buildView` were defined in the `$view->vars`, to ensure we are dealing with a `<select>` field built by a `ChoiceType`
* fetch recursively the value of the first displayed option (even with groups)

Commits
-------

b7f1ba4 [LiveComponent] Fix `ComponentWithFormTrait::extractFormValues()` with edge cases
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
4 participants