title: Symfony2 Pre populate form with data from model?
body: <p>I have a ProfileType as follows:</p>
  
  <pre><code>namespace Site\UserBundle\Form\Type;
  
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilder;
  
  class ProfileType extends AbstractType
  {
      public function buildForm(FormBuilder $builder, array $options)
      {       
          $builder-&gt;add('facebook', 'text', array('required'=&gt;false))
              -&gt;add('myspace', 'text', array('required'=&gt;false))
              -&gt;add('twitter', 'text', array('required'=&gt;false))
              -&gt;add('soundcloud', 'text', array('required'=&gt;false))
              -&gt;add('youtube', 'text', array('required'=&gt;false))
              -&gt;add('website', 'text', array('required'=&gt;false))
              -&gt;add('bio', 'textarea', array('required'=&gt;false));
      }
  
      public function getName()
      {
          return 'profile';
      }
  }
  </code></pre>
  
  <p>and I want to pre populate the form fields with data that is already in the database so it is visible in the form. </p>
  
  <p>My controller:</p>
  
  <pre><code>namespace Site\UserBundle\Controller;
  
  use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  use Symfony\Component\Security\Core\SecurityContext;
  use Symfony\Component\HttpFoundation\Request;
  use Site\UserBundle\Entity\Profile;
  use Site\UserBundle\Form\Type\ProfileType;
  
  class ProfileController extends Controller
  {
      public function editAction()
      {           
          $em = $this-&gt;getDoctrine()-&gt;getEntityManager();
          $editprofile = $em-&gt;getRepository('SiteUserBundle:Profile')-&gt;findOneByUser($user = $this-&gt;get('security.context')-&gt;getToken()-&gt;getUser()-&gt;getId());
  
          $form = $this-&gt;createForm(new ProfileType(), $editprofile);
  
          $form-&gt;bindRequest($this-&gt;getRequest());
              if ($form-&gt;isValid()) {
                  $editprofile-&gt;setUpdated(new \DateTime("now"));
                  $em-&gt;flush();
  
                  return $this-&gt;redirect($this-&gt;generateUrl('SiteUserBundle_login'));
              }
  
          return $this-&gt;render(
              'SiteUserBundle:Default:editprofile.html.twig', 
              array('form' =&gt; $form-&gt;createView())
          );
      }
  }
  </code></pre>
  
  <p>Any ideas? I thought this way would be easier to update a users profile.</p>