title: Silex - Symfony2 : Forms autofilled for text forms, not for choices
body: <p>First, best wishes to all for this 2016 new year !</p>
  
  <p>I'm facing a problem I did not manage to resolve on my own.</p>
  
  <p>I'm working on a Silex (<code>~1.3</code>) application. I coded simple CRUDs on my domain classes. I created as well some Type forms to be able to modify my basic domain classes. In this case, I have to manage the notion of <code>State</code> within a <code>Country</code>. Each is a specific class, and a <code>State</code> has one <code>Country</code> attribute.</p>
  
  <p>In my form, I declared some text fields, and a <code>Choice</code> field to be able to select the country (the form class is copied below).</p>
  
  <p>My problem is that when I try to modify an existing <code>State</code> with the following controller, the text fields <code>name</code>, <code>code</code>, <code>unloc</code> are filled with the data from the database, but not the choices <code>country</code> nor <code>hub</code> (the controller class is copied below).</p>
  
  <p>Please note that i'm NOT using Doctrine, but a home-made (and quite basic) DAO.</p>
  
  <p>Here is my code and some information :</p>
  
  <ol>
  <li><p>The view is done using twig, with the 'standard' bootstrap example which can be found here : <a href="http://symfony.com/doc/current/cookbook/form/form_customization.html" rel="nofollow" title="Form customization">Form Customization</a>, using <a href="https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_3_layout.html.twig" rel="nofollow" title="Bootstrap layout">Bootstrap layout</a> and <a href="https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig" rel="nofollow" title="Form layout">Form layout</a> : </p>
  
  <pre><code>{% extends 'layout.html.twig' %} {% block title %}{{ title }}{% endblock %}
  {% block content %}
      {% if form and is_granted('IS_AUTHENTICATED_FULLY') and is_granted('ROLE_ADMIN')%}
          {% form_theme form 'bootstrap_3_layout.html.twig' %} {{ form_start(form) }}
              &lt;div class="form-group"&gt;
                  {{ form_errors(form) }}
                  {{ form_widget(form) }}
                  &lt;input type="submit" class="btn btn-primary" value={% if button is not defined %} "Save"{% else %}"{{ button }}"{% endif %} /&gt;
              &lt;/div&gt;
          {{ form_end(form) }}
      {% else %}
          &lt;div&gt;
              &lt;p&gt;Ask an admin to add/modify information.&lt;/p&gt;
          &lt;/div&gt;
      {% endif %}
  {% endblock %}
  </code></pre></li>
  <li><p>composer.json content :</p>
  
  <pre><code>{
      "require": {
          "silex/silex": "~1.3",
          "doctrine/dbal": "2.5.*",
          "symfony/security": "2.7.*",
          "twig/twig": "1.21.*",
          "symfony/twig-bridge": "2.7.*",
          "symfony/form": "2.7.*",
          "symfony/translation": "2.7.*",
          "symfony/config": "2.7.*",
          "jasongrimes/silex-simpleuser": "*",
          "twig/extensions": "1.3.*",
          "symfony/validator": "2.*",
          "phpoffice/phpexcel": "1.*"
      },
      "require-dev": {
          "phpunit/phpunit": "*",
          "symfony/browser-kit": "*",
          "symfony/css-selector": "*",
          "silex/web-profiler": "*",
          "symfony/monolog-bridge": "*"
      },
      "autoload":{
          "psr-4":{"Easytrip2\\": "src"}
      }
  }
  </code></pre></li>
  <li><p>Form code :</p>
  
  <pre><code>&lt;?php
  
  namespace Easytrip2\Form\Type;
  
  use Easytrip2\DAO\CountryDAO;
  use Easytrip2\DAO\GeopointDAO;
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceList;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\OptionsResolver\OptionsResolver;
  
  class StateType extends AbstractType {
      /**
       * @CountryDAO
       * \Easytrip2\DAO\CountryDAO
       * allow to find the Country for the form.
       */
      private $countryDAO;
      /**
       * @GeopointDAO
       * \Easytrip2\DAO\GeopointDAO
       * allow to find the Country for the form.
       */
      private $geopointDAO;
      /**
       *
       * {@inheritDoc}
       *
       * @see \Symfony\Component\Form\AbstractType::buildForm()
       */
      public function buildForm(FormBuilderInterface $builder, array     $options) {
          $builder-&gt;add ( 'name', 'text', array (
                  'label' =&gt; 'State name'
          ) );
          $builder-&gt;add ( 'code', 'text', array (
                  'label' =&gt; 'State code'
          ) );
          $builder-&gt;add ( 'unloc', 'text', array (
                  'label' =&gt; 'State code'
          ) );
          $countries = $this-&gt;countryDAO-&gt;findAll ();
          $choices = array ();
          $labels = array ();
          foreach ( $countries as $value ) {
              $choices [] = $value;
              $labels [] = $value-&gt;getName ();
          }
          $builder-&gt;add ( 'country', 'choice', array (
                  'choice_list' =&gt; new ChoiceList ( $choices, $labels )
          ) );
  
          $hubs = $this-&gt;geopointDAO-&gt;findAllHubs ();
          $choices = array ();
          $labels = array ();
          foreach ( $hubs as $value ) {
              $choices [] = $value;
              $labels [] = $value-&gt;getName ();
          }
          $builder-&gt;add ( 'hub', 'choice', array (
                  'choice_list' =&gt; new ChoiceList ( $choices, $labels ),
                  'required' =&gt; false
          ) );
      }
  
      /**
       *
       * {@inheritDoc}
       *
       * @see \Symfony\Component\Form\AbstractType::configureOptions()
       */
      public function configureOptions(OptionsResolver $resolver) {
          $resolver-&gt;setDefaults ( array (
                  'data_class' =&gt; 'Easytrip2\Domain\State'
          ) );
      }
  
      /**
       *
       * {@inheritDoc}
       *
       * @see \Symfony\Component\Form\FormTypeInterface::getName()
       */
      public function getName() {
          return 'state';
      }
      /**
       * allow use of a CountryDAO
       */
      public function __construct(CountryDAO $countryDAO, GeopointDAO     $geopointDAO) {
          $this-&gt;geopointDAO = $geopointDAO;
          $this-&gt;countryDAO = $countryDAO;
      }
  }
  </code></pre></li>
  <li><p>Controller content :</p>
  
  <pre><code>public function stateUpdateByIdAction($id, Request $request, Application $app) {
          if ($app ['security.authorization_checker']-&gt;isGranted ( 'IS_AUTHENTICATED_FULLY' ) and $app ['security.authorization_checker']-&gt;isGranted ( 'ROLE_ADMIN' )) {
              $obj = $app ['dao.state']-&gt;findById ( $id );
              $form = $app ['form.factory']-&gt;create ( new StateType ( $app ['dao.country'], $app ['dao.geopoint'] ), $obj );
              $form-&gt;handleRequest ( $request );
              if ($form-&gt;isSubmitted () &amp;&amp; $form-&gt;isValid ()) {
                  if ($app ['dao.state']-&gt;save ( $obj )) {
                      $app ['session']-&gt;getFlashBag ()-&gt;add ( 'success', 'The state was succesfully updated.' );
                      return $app-&gt;redirect ( $app ['url_generator']-&gt;generate ( 'state' ) );
                  } else {
                      $app ['session']-&gt;getFlashBag ()-&gt;add ( 'error', 'Something went wrong...' );
                  }
              }
              return $app ['twig']-&gt;render ( 'form.html.twig', array (
                      'form' =&gt; $form-&gt;createView (),
                      'title' =&gt; 'Edit states'
              ) );
          } else {
              $app ['session']-&gt;getFlashBag ()-&gt;add ( 'error', 'Don\'t have the rights...' );
              return $app-&gt;redirect ( $app ['url_generator']-&gt;generate ( 'home' ) );
          }
      }
  </code></pre></li>
  </ol>