-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] Deprecated setting "choices_as_values" to "false" #16705
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
Merged
Merged
Conversation
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
Thank you @nicolas-grekas. |
fabpot
added a commit
that referenced
this pull request
Nov 27, 2015
…webmozart) This PR was merged into the 2.7 branch. Discussion ---------- [Form] Deprecated setting "choices_as_values" to "false" | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - This is #16681 for 2.7, i.e. without the deprecations trigger Commits ------- 3ab8189 [Form] Deprecated setting "choices_as_values" to "false"
Merged
fabpot
added a commit
that referenced
this pull request
Nov 6, 2016
…pe (yceruto) This PR was merged into the 2.7 branch. Discussion ---------- [Form] Fixed show float values as choice value in ChoiceType | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #13817 | License | MIT | Doc PR | - There is a closed [issue][1] related to this before Symfony 2.7 (when choice value still was the key from the `array`). This issue already happened in 2.7+ but now inside Symfony core. Information ----------- [This method][3] checks whether the given choices can be cast to strings without generating duplicates (3ab8189 in #16705): ```php private function castableToString(array $choices, array &$cache = array()) { foreach ($choices as $choice) { if (is_array($choice)) { if (!$this->castableToString($choice, $cache)) { return false; } continue; } elseif (!is_scalar($choice)) { return false; } elseif (isset($cache[$choice])) { // <---- red breakpoint return false; } $cache[$choice] = true; // <---- red breakpoint } return true; } ``` So it should to keep [scalar values (integer, float, string or boolean)][2] as choice values always (unless have duplicates values). The Problem ----------- But in this situation it doesn't happen: ```php $form = $this->createFormBuilder() ->add('foo', ChoiceType::class, [ 'choices' => [ 'Min' => 0.5, 'Mid' => 1.0, 'Max' => 1.5, ] ]) ->getForm(); ``` **Output:** ```html <select id="form_foo" name="form[foo]"> <option value="0">Min</option> <option value="1">Mid</option> <option value="2">Max</option> </select> ``` [**Why?**][5] If the key of the array is a float number, it's interpreted as integer automatically: ```php // ... $cache[$choice] = true; // when $choice = 0.5: $cache = [0 => true] // when $choice = 1.0: $cache = [0 => true, 1 => true] ``` Then, when `$choice = 1.5` [this sentence][4] `isset($cache[1.5])` returns `true` because really checks `isset($cache[1])` and this key already exists, so `castableToString()` returns `false` (detected as duplicate) and the choices values are generated incrementing integers as values. The PR's Effect ------------- **Before:** ```html <select id="form_foo" name="form[foo]"> <option value="0">Min</option> <option value="1">Mid</option> <option value="2">Max</option> </select> ``` **After:** ```html <select id="form_foo" name="form[foo]"> <option value="0.5">Min</option> <option value="1">Mid</option> <option value="1.5">Max</option> </select> ``` [1]: #13817 [2]: http://php.net/manual/en/function.is-scalar.php [3]: https://github.com/symfony/symfony/blob/2.7/src/Symfony/Component/Form/ChoiceList/ArrayChoiceList.php#L228 [4]: https://github.com/symfony/symfony/blob/2.7/src/Symfony/Component/Form/ChoiceList/ArrayChoiceList.php#L239 [5]: https://github.com/symfony/symfony/blob/2.7/src/Symfony/Component/Form/ChoiceList/ArrayChoiceList.php#L243 Commits ------- 3564228 [Form] Fix show float values as choices values in ChoiceType
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.
This is #16681 for 2.7, i.e. without the deprecations trigger