title: Symfony 2 | Form exception when modifying an object that has a file(picture) field
body: <p>I'm using Symfony2.
  I have an entity <b>Post</b> that has a title and a picture field.</p>
  
  <p><b> My problem : </b>Everything is fine when I create a post, I have my picture etc. But when I want to modify it, I have a problem with the "picture" field which is an uploaded file, Symfony wants a file type and it has a string (the path of the uploaded file) :</p>
  
  <pre><code>The form's view data is expected to be an instance of class Symfony\Component\HttpFoundation\File\File, but is a(n) string. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms a(n) string to an instance of Symfony\Component\HttpFoundation\File\File. 
  </code></pre>
  
  <p>I'm really stuck with this problem and really don't know how to solve it, any help would be greatly appreciated! Thanks a lot!</p>
  
  <p>Here is my <b>PostType.php</b> (which is used in newAction() and modifiyAction()) and which may cause the problem (<i>Form/PostType.php</i>) :</p>
  
  <pre><code>&lt;?php
  namespace MyBundle\Form;
  
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\HttpFoundation\File\UploadedFile;
  
  use MyBundle\Entity\Post;
  
  class PostType extends AbstractType
  {
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
          -&gt;add('title')
          -&gt;add('picture', 'file');//there is a problem here when I call the modifyAction() that calls the PostType file.
      }
  
      public function getDefaultOptions(array $options)
      {
          return array(
              'data_class' =&gt; 'MyBundle\Entity\Post',
          );
      }
  
      public static function processImage(UploadedFile $uploaded_file, Post $post)
      {
          $path = 'pictures/blog/';
          //getClientOriginalName() =&gt; Returns the original file name.
          $uploaded_file_info = pathinfo($uploaded_file-&gt;getClientOriginalName());
          $file_name =
              "post_" .
              $post-&gt;getTitle() .
              "." .
              $uploaded_file_info['extension']
              ;
  
          $uploaded_file-&gt;move($path, $file_name);
  
          return $file_name;
      }
  
      public function getName()
      {
          return 'form_post';
      }
  }
  </code></pre>
  
  <p>Here is my <b>Post entity</b> (<i>Entity/Post.php</i>) :</p>
  
  <pre><code>&lt;?php
  
  namespace MyBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  
  use Symfony\Component\Validator\Constraints as Assert;
  
  /**
   * MyBundle\Entity\Post
   *
   * @ORM\Table()
   * @ORM\Entity
   */
  class Post
  {
      /**
       * @var integer $id
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
      /**
       * @ORM\Column(type="string", length=255, nullable=true)
       * @Assert\Image(
       *      mimeTypesMessage = "Not valid.",
       *      maxSize = "5M",
       *      maxSizeMessage = "Too big."
       *      )
       */
      private $picture;
  
      /**
       * @var string $title
       *
       * @ORM\Column(name="title", type="string", length=255)
       */
      private $title;
  
     //getters and setters
     }
  </code></pre>
  
  <p>Here is my <b>newAction()</b> (<i>Controller/PostController.php</i>) <b><i>Every works fine with this function</i></b>:</p>
  
  <pre><code>public function newAction()
  {
      $em = $this-&gt;getDoctrine()-&gt;getEntityManager();
      $post = new Post();
      $form = $this-&gt;createForm(new PostType, $post);
      $post-&gt;setPicture("");
      $form-&gt;setData($post);
      if ($this-&gt;getRequest()-&gt;getMethod() == 'POST') 
      {
          $form-&gt;bindRequest($this-&gt;getRequest(), $post);
          if ($form-&gt;isValid()) 
          {
              $uploaded_file = $form['picture']-&gt;getData();
              if ($uploaded_file) 
              {
                  $picture = PostType::processImage($uploaded_file, $post);
                  $post-&gt;setPicture('pictures/blog/' . $picture);
              }
              $em-&gt;persist($post);
              $em-&gt;flush();
              $this-&gt;get('session')-&gt;setFlash('succes', 'Post added.');
  
              return $this-&gt;redirect($this-&gt;generateUrl('MyBundle_post_show', array('id' =&gt; $post-&gt;getId())));
          }
      }
  
      return $this-&gt;render('MyBundle:Post:new.html.twig', array('form' =&gt; $form-&gt;createView()));
  }
  </code></pre>
  
  <p>Here is my <b>modifyAction()</b> (<i>Controller/PostController.php</i>) :<b><i>There is a problem with this function</i></b></p>
  
  <pre><code>public function modifyAction($id)
  {
      $em = $this-&gt;getDoctrine()-&gt;getEntityManager();
      $post = $em-&gt;getRepository('MyBundle:Post')-&gt;find($id);
      $form = $this-&gt;createForm(new PostType, $post);//THIS LINE CAUSES THE EXCEPTION
      if ($this-&gt;getRequest()-&gt;getMethod() == 'POST') 
      {
          $form-&gt;bindRequest($this-&gt;getRequest(), $post);
          if ($form-&gt;isValid()) 
          {
              $uploaded_file = $form['picture']-&gt;getData();
              if ($uploaded_file) 
              {
                  $picture = PostType::processImage($uploaded_file, $post);
                  $post-&gt;setPicture('pictures/blog/' . $picture);
              }
              $em-&gt;persist($post);
              $em-&gt;flush();
              $this-&gt;get('session')-&gt;setFlash('succes', 'Modifications saved.');
  
              return $this-&gt;redirect($this-&gt;generateUrl('MyBundle_post_show', array('id' =&gt; $post-&gt;getId())));
          }
      }
      return $this-&gt;render('MyBundle:Post:modify.html.twig', array('form' =&gt; $form-&gt;createView(), 'post' =&gt; $post));
  }
  </code></pre>