[LiveComponent] Handle loose comparison with empty placeholder #2426
Merged
Kocal merged 1 commit intoDec 7, 2024
Conversation
c72be99 to
3965302
Compare
Member
|
Thank you @FoxCie. |
Author
|
Thank you for the quick merge! |
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
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 (
falseindicates 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, ornullif there is no placeholder specified. From this, I considered that there is no placeholder either if the placeholder holdsfalseornull.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.
#SymfonyHackday :)