<p dir="auto">Symfony choice fields with <code class="notranslate">expanded</code> set to <code class="notranslate">true</code> don’t render the selected option correctly when the value in the underlying entity is <code class="notranslate">false</code>.</p>
<p dir="auto">This affects Symfony 2.7.</p>
<h2 dir="auto">Steps to reproduce</h2>
<p dir="auto">Create the following entity:</p>
<div class="highlight highlight-text-html-php notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="class Foo
{
    public $bar = false;
}"><pre class="notranslate"><span class="pl-k">class</span> <span class="pl-v">Foo</span>
{
    <span class="pl-k">public</span> <span class="pl-c1"><span class="pl-c1">$</span>bar</span> = <span class="pl-c1">false</span>;
}</pre></div>
<p dir="auto">Create the following form:</p>
<div class="highlight highlight-text-html-php notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="$form = $this-&gt;createFormBuilder(new Foo())
    -&gt;add('bar', 'choice', array(
        'choices' =&gt; array(
            1 =&gt; 'Yes', // boolean `true` and `false` as keys also exhibit the same problem
            0 =&gt; 'No',
        ),
        'expanded' =&gt; true,
    ))
    -&gt;getForm()
;

return $this-&gt;render('AppBundle::form.html.twig', array(
    'form' =&gt; $form-&gt;createView(),
));"><pre class="notranslate"><span class="pl-s1"><span class="pl-c1">$</span>form</span> = <span class="pl-s1"><span class="pl-c1">$</span><span class="pl-smi">this</span></span>-&gt;<span class="pl-en">createFormBuilder</span>(<span class="pl-k">new</span> <span class="pl-v">Foo</span>())
    -&gt;<span class="pl-en">add</span>(<span class="pl-s">'bar'</span>, <span class="pl-s">'choice'</span>, <span class="pl-en">array</span>(
        <span class="pl-s">'choices'</span> =&gt; <span class="pl-en">array</span>(
            <span class="pl-c1">1</span> =&gt; <span class="pl-s">'Yes'</span>, <span class="pl-c">// boolean `true` and `false` as keys also exhibit the same problem</span>
            <span class="pl-c1">0</span> =&gt; <span class="pl-s">'No'</span>,
        ),
        <span class="pl-s">'expanded'</span> =&gt; <span class="pl-c1">true</span>,
    ))
    -&gt;<span class="pl-en">getForm</span>()
;

<span class="pl-k">return</span> <span class="pl-s1"><span class="pl-c1">$</span><span class="pl-smi">this</span></span>-&gt;<span class="pl-en">render</span>(<span class="pl-s">'AppBundle::form.html.twig'</span>, <span class="pl-en">array</span>(
    <span class="pl-s">'form'</span> =&gt; <span class="pl-s1"><span class="pl-c1">$</span>form</span>-&gt;<span class="pl-en">createView</span>(),
));</pre></div>
<p dir="auto">Create the following template:</p>
<div class="highlight highlight-text-html-php notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="{{ form_start(form) }}
    {{ form_widget(form.bar) }}
{{ form_end(form) }}"><pre class="notranslate">{{ form_start(form) }}
    {{ form_widget(form.bar) }}
{{ form_end(form) }}</pre></div>
<p dir="auto">Expected result: the “no” radio button to be checked.<br>
Actual result: none of the checkboxes are checked.</p>
<p dir="auto">Changing the value of <code class="notranslate">Foo::$bar</code> to <code class="notranslate">true</code> makes the “yes” radio button correctly render in its selected state.</p>
<p dir="auto">When the <code class="notranslate">expanded</code> option on the choice field is changed to <code class="notranslate">false</code>, an HTML <code class="notranslate">select</code> menu is correctly rendered with the “no” option selected.</p>
<p dir="auto">The exact same effect can be observed when the keys in the <code class="notranslate">choices</code> array are boolean <code class="notranslate">true</code> and <code class="notranslate">false</code> instead of <code class="notranslate">1</code> and <code class="notranslate">0</code>.</p>
<p dir="auto">I have a form with the following code:</p>
<div class="highlight highlight-text-html-php notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="$form = $this-&gt;factory-&gt;create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array(
  'multiple' =&gt; false,
  'expanded' =&gt; false,
  'choices' =&gt; array(
      'label a' =&gt; false,
      'label b' =&gt; true,
  ),
  'choices_as_values' =&gt; true,
));"><pre class="notranslate"><span class="pl-s1"><span class="pl-c1">$</span>form</span> = <span class="pl-s1"><span class="pl-c1">$</span><span class="pl-smi">this</span></span>-&gt;<span class="pl-c1">factory</span>-&gt;<span class="pl-en">create</span>(<span class="pl-s">'Symfony\Component\Form\Extension\Core\Type\ChoiceType'</span>, <span class="pl-c1">null</span>, <span class="pl-en">array</span>(
  <span class="pl-s">'multiple'</span> =&gt; <span class="pl-c1">false</span>,
  <span class="pl-s">'expanded'</span> =&gt; <span class="pl-c1">false</span>,
  <span class="pl-s">'choices'</span> =&gt; <span class="pl-en">array</span>(
      <span class="pl-s">'label a'</span> =&gt; <span class="pl-c1">false</span>,
      <span class="pl-s">'label b'</span> =&gt; <span class="pl-c1">true</span>,
  ),
  <span class="pl-s">'choices_as_values'</span> =&gt; <span class="pl-c1">true</span>,
));</pre></div>
<p dir="auto">When submitting the form, none of the two radio buttons are selected, i.e. the values are lost.</p>
<p dir="auto">I've written a unit test for it in <a class="commit-link" data-hovercard-type="commit" data-hovercard-url="https://github.com/imphil/symfony/commit/71a59b79fe2afb5db1fd5e5e28f46b52b7e961cd/hovercard" href="https://github.com/imphil/symfony/commit/71a59b79fe2afb5db1fd5e5e28f46b52b7e961cd">imphil@<tt>71a59b7</tt></a></p>
<p dir="auto">Currently it fails with</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="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"><pre class="notranslate"><code class="notranslate">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
</code></pre></div>
<p dir="auto">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?</p>
<p dir="auto">(This seems to be similar to <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="85524044" data-permission-text="Title is private" data-url="https://github.com/symfony/symfony/issues/14877" data-hovercard-type="issue" data-hovercard-url="/symfony/symfony/issues/14877/hovercard" href="https://github.com/symfony/symfony/issues/14877">#14877</a>, but maybe not exactly the same, so I didn't want to hijack that issue.)</p>