Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[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

Merged
merged 2 commits into from
Apr 28, 2016

Conversation

HeahDude
Copy link
Contributor

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 -

ChoiceType now always use ChoiceToValueTransformer or ChoicesToValuesTransformer depending on multiple option.
Hence CheckboxListMapper and RadioListMapper don't handle the transformation anymore.

@@ -1247,7 +1279,7 @@ public function testSubmitSingleExpandedWithEmptyChild()

$form->submit('');

$this->assertNull($form->getData());
$this->assertEmpty($form->getData());
Copy link
Contributor Author

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 :)

Copy link
Contributor Author

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).

Copy link
Contributor Author

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 :)

@HeahDude HeahDude force-pushed the fix-form-radio_list_mapper branch 2 times, most recently from 137bf44 to 5cc6bb4 Compare March 15, 2016 16:55
@javiereguiluz
Copy link
Member

@HeahDude thanks for this fix (and for everything you do for the Form component).

Just a minor comment: it seems that the | Bug fix? | yes/no row disappeared from the PR description table (it is usually displayed after the Branch? row). This information is important for mergers. Could you please include it? Thanks!

@HeahDude
Copy link
Contributor Author

@javiereguiluz done :)

@HeahDude HeahDude force-pushed the fix-form-radio_list_mapper branch from 5cc6bb4 to 76e265c Compare March 15, 2016 17:19
HeahDude referenced this pull request Mar 16, 2016
This fixes "false" choice pre selection when `ChoiceType`
is `expanded` and not `multiple`
@HeahDude HeahDude force-pushed the fix-form-radio_list_mapper branch from 76e265c to 01b8101 Compare March 16, 2016 10:24
{
if (!is_string($choice)) {
Copy link
Contributor Author

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 ?

Copy link
Contributor Author

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.

@HeahDude
Copy link
Contributor Author

Failures seem unrelated.

@nakashu
Copy link

nakashu commented Mar 17, 2016

👍

}

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);
Copy link
Member

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

Copy link
Contributor Author

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.

Copy link
Contributor

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? :)

Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fair enough then.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

@HeahDude HeahDude force-pushed the fix-form-radio_list_mapper branch 2 times, most recently from aa6fae7 to 695b5b1 Compare March 17, 2016 13:09
? new CheckboxListMapper($options['choice_list'])
: new RadioListMapper($options['choice_list']));
? new CheckboxListMapper()
: new RadioListMapper());
Copy link
Contributor Author

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 ?

Copy link
Member

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 :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done :)

@HeahDude HeahDude force-pushed the fix-form-radio_list_mapper branch 2 times, most recently from 3f2b402 to 103566e Compare March 17, 2016 13:35
@HeahDude
Copy link
Contributor Author

Comments addressed.

@HeahDude
Copy link
Contributor Author

TwigBridge dependency to EventDispatcher seems to be broken with php 7 build in travis.

@xabbuh
Copy link
Member

xabbuh commented Mar 19, 2016

@HeahDude Can you check if rebasing fixes the tests?

@HeahDude HeahDude force-pushed the fix-form-radio_list_mapper branch from 103566e to 58e8ed0 Compare March 19, 2016 12:09
@HeahDude
Copy link
Contributor Author

@xabbuh It did. Thanks!

}
} elseif ($options['multiple']) {
// <select> tag with "multiple" option
if ($options['multiple']) {
Copy link
Contributor

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');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

same here

@HeahDude HeahDude force-pushed the fix-form-radio_list_mapper branch from 58e8ed0 to b3fa216 Compare April 1, 2016 08:51
@HeahDude
Copy link
Contributor Author

HeahDude commented Apr 1, 2016

Ok there was a reason why I use assertEmpty, it's because $extraData in as empty array while $viewData is an empty string.

I fixed the assertions accordingly.

Rebased and squashed! Needs a final review here. Thanks!

@HeahDude HeahDude force-pushed the fix-form-radio_list_mapper branch from b3fa216 to c77ae14 Compare April 1, 2016 09:12
@HeahDude
Copy link
Contributor Author

HeahDude commented Apr 1, 2016

There is a little failure in Doctrine bridge because it needs that PR to be merged first.

I've updated the dependency to 2.7.12 but travis does not seem to be aware of it yet.

Is it OK ?

@HeahDude HeahDude changed the title [Form] fix pre selection of choices with ChoiceType [Form] [BC Break] fix pre selection of choices with ChoiceType Apr 1, 2016
@HeahDude HeahDude changed the title [Form] [BC Break] fix pre selection of choices with ChoiceType [Form] fixed BC break with pre selection of choices with ChoiceType and its children Apr 1, 2016
@@ -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",
Copy link
Member

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?

@HeahDude
Copy link
Contributor Author

HeahDude commented Apr 1, 2016

@xabbuh Done, let's wait & see :)

@HeahDude
Copy link
Contributor Author

HeahDude commented Apr 1, 2016

Nope :(

@HeahDude
Copy link
Contributor Author

HeahDude commented Apr 1, 2016

@xabbuh Can this be related to the last releases messed tags ?

@xabbuh
Copy link
Member

xabbuh commented Apr 1, 2016

@HeahDude This looks good now (the deps=low job is passing).

@HeahDude
Copy link
Contributor Author

HeahDude commented Apr 1, 2016

Great!

@dmaicher
Copy link
Contributor

dmaicher commented Apr 8, 2016

Any news here? Waiting for this fix so I can upgrade to Symfony 2.8.* :)

HeahDude added 2 commits April 9, 2016 15:28
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.
@HeahDude HeahDude force-pushed the fix-form-radio_list_mapper branch from 9d2dc37 to ea5375c Compare April 9, 2016 13:29
@fabpot
Copy link
Member

fabpot commented Apr 28, 2016

Thank you @HeahDude.

@fabpot fabpot merged commit ea5375c into symfony:2.7 Apr 28, 2016
fabpot added a commit that referenced this pull request Apr 28, 2016
…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()`"
@HeahDude HeahDude deleted the fix-form-radio_list_mapper branch April 28, 2016 10:31
HeahDude added a commit to HeahDude/symfony that referenced this pull request Apr 28, 2016
fabpot pushed a commit that referenced this pull request Apr 28, 2016
fabpot added a commit that referenced this pull request Apr 28, 2016
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
fabpot added a commit that referenced this pull request Apr 28, 2016
* 3.0:
  [Form] Fix tests added in #18180
@HeahDude
Copy link
Contributor Author

@dmaicher, happy upgrade! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants