title: Symfony 2 prefilled form with data from the database
body: <p>I am trying to populate form fields with data from the database in order to edit them. I already searched on google.</p>
  
  <p>Here is my controller which returns empty fields</p>
  
  <pre><code>public function userViewAction($id,Request $request){
  
      $em = $this-&gt;getDoctrine()-&gt;getManager()-&gt;getRepository('BFVMailingBundle:MailingList');
      $user = $em-&gt;findById($id);
  
      $form = $this-&gt;get('form.factory')-&gt;createBuilder('form',$user)
          -&gt;add('unsubscribed','checkbox')
          -&gt;add('name','text')
          -&gt;add('givenName','text')
          -&gt;add('additionalName','text',array('required'=&gt;false))
          -&gt;add('familyName','text',array('required'=&gt;false))
          -&gt;add('emailValue','text')
          -&gt;add('language','choice',array(
              'choices'  =&gt; array('en_GB' =&gt; 'en_GB', 'es_ES' =&gt; 'es_ES', 'fr_FR' =&gt; 'fr_FR'),
              'required' =&gt; true,
          ))
          -&gt;add('commentary','textarea',array('required'=&gt;false))
          -&gt;add('save','submit')
          -&gt;getForm();
  
  
      if ($request-&gt;getMethod() == 'POST') {
          $form-&gt;bindRequest($request);
  
          if ($form-&gt;isValid()) {
              // perform some action, such as save the object to the database
              $em-&gt;flush();
              return $this-&gt;redirect($this-&gt;generateUrl('user_view',array('id'=&gt;$id)));
          }
      }
  </code></pre>
  
  <p>and this is my template</p>
  
  <pre><code>&lt;div class="cell"&gt;  
      {{ form_start(form, {'attr': {'class': 'form-horizontal'}}) }}
      {{ form_end(form) }}
  &lt;/div&gt;
  </code></pre>
  
  <p>Did I miss something?</p>
  
  <p><strong>EDIT - READ THIS FOR THE SOLUTION</strong></p>
  
  <p>As John Noel Implied I build an externalised form with the command</p>
  
  <pre><code>php app/console doctrine:generate:form BFVMailingBundle:MailingList
  </code></pre>
  
  <p><em>my entity was MailingList instead of User</em></p>
  
  <p>the MailingListType is a form template which is generated in BFV\MailingBundle\Form. I've added the data types myself.</p>
  
  <pre><code>&lt;?php
  namespace BFV\MailingBundle\Form;
  
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\OptionsResolver\OptionsResolverInterface;
  
  class MailingListType extends AbstractType
  {
  /**
   * @param FormBuilderInterface $builder
   * @param array $options
   */
  public function buildForm(FormBuilderInterface $builder, array $options)
  {
      $str = date("U");
      $codeValue = sha1($str);
  
      $builder
          -&gt;add('secretCode','hidden',array( 'data' =&gt; $codeValue ))
          -&gt;add('name','text')
          -&gt;add('givenName','text')
          -&gt;add('additionalName','text',array('required'=&gt;false))
          -&gt;add('familyName','text',array('required'=&gt;false))
          -&gt;add('emailValue','text')
          -&gt;add('language','choice',array(
              'choices'  =&gt; array('en_GB' =&gt; 'en_GB', 'es_ES' =&gt; 'es_ES', 'fr_FR' =&gt; 'fr_FR'),
              'required' =&gt; true,
          ))
          -&gt;add('unsubscribed','checkbox')
          -&gt;add('commentary','textarea',array('required'=&gt;false))
          -&gt;add('save','submit')
      ;
  }
  
  /**
   * @param OptionsResolverInterface $resolver
   */
  public function setDefaultOptions(OptionsResolverInterface $resolver)
  {
      $resolver-&gt;setDefaults(array(
          'data_class' =&gt; 'BFV\MailingBundle\Entity\MailingList'
      ));
  }
  
  /**
   * @return string
   */
  public function getName()
  {
      return 'bfv_mailingbundle_mailinglist';
  }
  }
  </code></pre>
  
  <p>In the reformated controller I add to add the the form generator the instance of MailingList $user[0] instead of $user. I read in many websites that usually you put $variable directly in the form builder but that generated the following error:</p>
  
  <pre><code>The form's view data is expected to be an instance of class 
  BFV\MailingBundle\Entity\MailingList, but is a(n) array. You can avoid 
  this error by setting the "data_class" option to null or by adding a
  view transformer that transforms a(n) array to an instance of 
  BFV\MailingBundle\Entity\MailingList
  </code></pre>
  
  <p>Thus in the controller:</p>
  
  <pre><code>    public function userViewAction($id,Request $request){
      if (!$id) {
          throw $this-&gt;createNotFoundException('No id !!');
      }
  
      $em = $this-&gt;getDoctrine()-&gt;getManager()-&gt;getRepository('BFVMailingBundle:MailingList');
      $user = $em-&gt;findById($id);
      if (!$user){
          throw $this-&gt;createNotFoundException('No user with the id selected');
      }
  
      $form = $this-&gt;createForm(new MailingListType(), $user[0]);
  
      if ($request-&gt;getMethod() == 'POST') {
          $form-&gt;bindRequest($request);
  
          if ($form-&gt;isValid()) {
              $em-&gt;flush();
              return $this-&gt;redirect($this-&gt;generateUrl('user_view',array('id'=&gt;$id)));
          }
      }
  
      return $this-&gt;render('BFVMailingBundle:Default:user_view.html.twig',array(
          'user'=&gt;$user,
          'form'=&gt;$form-&gt;createView()
              ));
  }
  </code></pre>
  
  <p>Conclusion: I got the view form rendering with populated data from the database.</p>