8000 Form: choice with expanded=true does not retain boolean values · Issue #15573 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Form: choice with expanded=true does not retain boolean values #15573

New issue
8000

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

Closed
imphil opened this issue Aug 17, 2015 · 10 comments
Closed

Form: choice with expanded=true does not retain boolean values #15573

imphil opened this issue Aug 17, 2015 · 10 comments
Labels

Comments

@imphil
Copy link
imphil commented Aug 17, 2015

I have a form with the following code:

$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array(
  'multiple' => false,
  'expanded' => false,
  'choices' => array(
      'label a' => false,
      'label b' => true,
  ),
  'choices_as_values' => true,
));

When submitting the form, none of the two radio buttons are selected, i.e. the values are lost.

I've written a unit test for it in imphil@71a59b7

Currently it fails with

There were 2 failures:

1) Symfony\Component\Form\Tests\Extension\Core\Type\ChoiceTypeTest::testSubmitSingleExpandedBooleanValueFalse
Failed asserting that false is identical to '0'.

/home/philipp/src/symfony/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php:354

2) Symfony\Component\Form\Tests\Extension\Core\Type\ChoiceTypeTest::testSubmitSingleExpandedBooleanValueTrue
Failed asserting that true is identical to '1'.

/home/philipp/src/symfony/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php:373

The problem only happens with boolean values and expanded=true. It seems that a proper conversion to the view data is missing. Unfortunately I have no idea about the inner workings of Symfony -- maybe someone with a bit more experience can have a look?

(This seems to be similar to #14877, but maybe not exactly the same, so I didn't want to hijack that issue.)

@imphil
Copy link
Author
imphil commented Aug 17, 2015

It's always the same ... I've found the issue already reported: #14712 Sorry for the duplicate, but feel free to take the unit test :)

@stof
Copy link
Member
stof commented Aug 27, 2015

I found a workaround for that: attaching a NoopDataTransformer on the choice field (a DataTransformer doing nothing and just returning the value as it).

Note that in case your choice field is not required, you will still need a custom choice_value callback to distinguish null and false (which are both casted to an empty string with the default callback).

@alexislefebvre
Copy link
Contributor

Thanks stof, this workaround worked for me! 👍

@xabbuh
Copy link
Member
xabbuh commented Oct 5, 2015

This behaviour seems to be related to what @DancZer observed in #5906 (comment).

@javiereguiluz
Copy link
Member

Closing it because, as @imphil said, this is a duplicate of #14712.

@alexislefebvre
Copy link
Contributor
alexislefebvre commented May 9, 2016

@stof Note that the workaround doesn't work with Symfony 2.8.5; probably after the #18180 PR. I had to remove the workaround NoOp…, invert the keys and values in choices and add 'choices_as_values' => true, (second block in the documentation).

@HeahDude
Copy link
Contributor
HeahDude commented May 9, 2016

@alexislefebvre Yes #18180 fixes it once and for all ;)

@alexislefebvre
Copy link
Contributor

@HeahDude Yes, but it also broke the workaround, so I wanted to inform other users just in case they missed this information.

@HeahDude
Copy link
Contributor
HeahDude commented May 9, 2016

You're right, I wasn't questioning it :) Thanks!

@stof
Copy link
Member
stof commented May 9, 2016

@alexislefebvre my workaround was for a case where the choices_as_values was already true (see the original ticket).
Passing boolean choices as key never worked, because booleans cannot be used as array keys in PHP, and so your choices were '1' and '' instead (i.e. their string representation)

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

No branches or pull requests

6 participants
0