-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] fixed BC break with pre selection of choices with ChoiceType
and its children
#18180
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
Conversation
@@ -1247,7 +1279,7 @@ public function testSubmitSingleExpandedWithEmptyChild() | |||
|
|||
$form->submit(''); | |||
|
|||
$this->assertNull($form->getData()); | |||
$this->assertEmpty($form->getData()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like a collateral fix to me :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@webmozart you need to read this test in particular, the others are just using an empty string instead of null
as viewData
but this one concerns the data.
Also this PR fixes the failed tests added originally in #17760 (ref https://github.com/symfony/symfony/pull/17760/files#diff-533d45d8bb41e489890ad12df7a1a4b7R163).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This time it just doesn't break the data transformers call :)
137bf44
to
5cc6bb4
Compare
@HeahDude thanks for this fix (and for everything you do for the Form component). Just a minor comment: it seems that the |
@javiereguiluz done :) |
5cc6bb4
to
76e265c
Compare
This fixes "false" choice pre selection when `ChoiceType` is `expanded` and not `multiple`
76e265c
to
01b8101
Compare
{ | ||
if (!is_string($choice)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should I test null of force a string casting before throwing here ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@webmozart I re-check the code and here the view data should always be a string.
In contrary, since mapsFormsToData()
happens before transformation we should accept null
there as well.
Do you agree or am I missing something ? Thanks.
Failures seem unrelated. |
👍 |
} | ||
|
||
foreach ($checkboxes as $checkbox) { | ||
$value = $checkbox->getConfig()->getOption('value'); | ||
$checkbox->setData(isset($valueMap[$value]) ? true : false); | ||
$checkbox->setData(in_array($value, $choices, true) ? true : false); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the ternary operator turning a boolean into the same boolean looks weird to me
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@stof I first felt the same, as you can see I did to match the previous style (in both checkbox and radio list mappers).
I guess it's a kind of explicit casting since this is where choices string values are converted to their boolean "selected" state values.
Thanks for the review.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But technically
in_array($value, $choices, true) ? true : false
is the same as
in_array($value, $choices, true)
or am I missing something? :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would indeed remove those unneeded ternary operators when the left side is already a bool.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
aa6fae7
to
695b5b1
Compare
? new CheckboxListMapper($options['choice_list']) | ||
: new RadioListMapper($options['choice_list'])); | ||
? new CheckboxListMapper() | ||
: new RadioListMapper()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Little detail, it's now short enough to be inlined here imho, isn't it ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could have been inlined before as well :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done :)
3f2b402
to
103566e
Compare
Comments addressed. |
|
@HeahDude Can you check if rebasing fixes the tests? |
103566e
to
58e8ed0
Compare
@xabbuh It did. Thanks! |
} | ||
} elseif ($options['multiple']) { | ||
// <select> tag with "multiple" option | ||
if ($options['multiple']) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't fully understand why this is necessary. It seems to me the transformer and the data mappers are doing the same thing already. Can you add some explanation?
$e->getCode(), | ||
$e | ||
); | ||
throw new UnexpectedTypeException($choices, 'array'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here
58e8ed0
to
b3fa216
Compare
Ok there was a reason why I use I fixed the assertions accordingly. Rebased and squashed! Needs a final review here. Thanks! |
b3fa216
to
c77ae14
Compare
There is a little failure in Doctrine bridge because it needs that PR to be merged first. I've updated the dependency to Is it OK ? |
ChoiceType
ChoiceType
ChoiceType
ChoiceType
and its children
@@ -22,7 +22,7 @@ | |||
"require-dev": { | |||
"symfony/stopwatch": "~2.2", | |||
"symfony/dependency-injection": "~2.2", | |||
"symfony/form": "~2.7,>=2.7.1", | |||
"symfony/form": "~2.7,>=2.7.12", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you try ~2.7.12|~2.8.5
please?
@xabbuh Done, let's wait & see :) |
Nope :( |
@xabbuh Can this be related to the last releases messed tags ? |
@HeahDude This looks good now (the |
Great! |
Any news here? Waiting for this fix so I can upgrade to Symfony 2.8.* :) |
closes symfony#18173. This reverts commit 8f918e5.
fixes symfony#14712 and symfony#17789. `ChoiceType` now always use `ChoiceToValueTransformer` or `ChoicesToValuesTransformer` depending on `multiple` option. Hence `CheckboxListMapper` and `RadioListMapper` don’t handle the transformation anymore. Fixes pre selection of choice with model values such as `null`, `false` or empty string.
9d2dc37
to
ea5375c
Compare
Thank you @HeahDude. |
…ChoiceType` and its children (HeahDude) This PR was merged into the 2.7 branch. Discussion ---------- [Form] fixed BC break with pre selection of choices with `ChoiceType` and its children | Q | A | ------------- | --- | Branch | 2.7+ | Bugfix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #18173, #14712, #17789 | License | MIT | Doc PR | - - f7eea72 reverts BC break introduced in #17760 - 58e8ed0 fixes pre selection of choice with model values such as `false`, `null` or empty string without BC break. `ChoiceType` now always use `ChoiceToValueTransformer` or `ChoicesToValuesTransformer` depending on `multiple` option. Hence `CheckboxListMapper` and `RadioListMapper` don't handle the transformation anymore. Commits ------- ea5375c [Form] refactor CheckboxListMapper and RadioListMapper 71841c7 Revert "[Form] refactor `RadioListMapper::mapDataToForm()`"
This PR was submitted for the master branch but it was merged into the 3.0 branch instead (closes #18663). Discussion ---------- [Form] Fix tests added in #18180 | Q | A | ------------- | --- | Branch? | 3.0+ | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | ~ | License | MIT | Doc PR | ~ Commits ------- c45a435 [Form] Fix tests added in #18180
* 3.0: [Form] Fix tests added in #18180
@dmaicher, happy upgrade! :) |
false
,null
or empty string without BC break.ChoiceType
now always useChoiceToValueTransformer
orChoicesToValuesTransformer
depending onmultiple
option.Hence
CheckboxListMapper
andRadioListMapper
don't handle the transformation anymore.