title: symfony2 file lost upon form error
body: <p>I am using a standard implementation of file upload in connection with doctrine, as per the example on the symfony2 website tutorials.</p>
  
  <p>When my upload form encounters an error in validation, and sends the user back to the form with error messages, it looses the file chosen for upload, although if I var_dump my $entity->file I can see that it has the file...</p>
  
  <pre><code>    //if form is valid, do some stuff... if not:
      else {
  
          //var_dump($entity-&gt;file); //This works, I get my file
          //die;
  
          //Get and check the folder chosen as parent
          $entity-&gt;setFolder( $this-&gt;checkFolderId($request-&gt;request-&gt;get('folder')) ); //will cause die() if folder doesn't belong to this company
  
          $folders = $this-&gt;getFolders();
  
          return $this-&gt;render('BizTVMediaManagementBundle:Image:new.html.twig', array(
              'entity' =&gt; $entity,
              'form'   =&gt; $form-&gt;createView(),
              'folders' =&gt; $folders,
              'fileExists' =&gt; $fileExists,
          ));
  
      }
  </code></pre>
  
  <p>After this is put to the twig view, there is nothing in the file field.</p>
  
  <p>Here is my entity...</p>
  
  <pre><code>&lt;?php
  
  namespace BizTV\MediaManagementBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  use Symfony\Component\Validator\Constraints as Assert;
  
  /**
   * BizTV\MediaManagementBundle\Entity\Image
   *
   * @ORM\Table(name="image")
   * @ORM\Entity
   * @ORM\HasLifecycleCallbacks
   */
  class Image
  {
      /**
       * @var integer $id
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
      /**
       * @var string $name
  
       *
       * @ORM\Column(name="name", type="string", length=255)
       * @Assert\NotBlank
       */
      private $name;
  
      /**
       * @var integer $width
       *
       * @ORM\Column(name="width", type="integer")
       */
      private $width;
  
      /**
       * @var integer $height
       *
       * @ORM\Column(name="height", type="integer")
       */
      private $height;
  
      /**
       * @ORM\Column(type="string", length=255, nullable=true)
       */
      private $path;
  
      /**
      * @var object BizTV\BackendBundle\Entity\company
      *  
      * @ORM\ManyToOne(targetEntity="BizTV\BackendBundle\Entity\company")
      * @ORM\JoinColumn(name="company", referencedColumnName="id", nullable=false)
      */
      protected $company;     
  
      /**
      * @var object BizTV\MediaManagementBundle\Entity\Folder
      *  
      * @ORM\ManyToOne(targetEntity="BizTV\MediaManagementBundle\Entity\Folder")
      * @ORM\JoinColumn(name="folder", referencedColumnName="id", nullable=true)
      */
      protected $folder;
  
  
      /**
       * @Assert\File(maxSize="6000000")
       */
       public $file;
  
  
      /**
       * @ORM\PrePersist()
       * @ORM\PreUpdate()
       */
      public function preUpload()
      {
          if (null !== $this-&gt;file) {
              // do whatever you want to generate a unique name
              $this-&gt;path = sha1(uniqid(mt_rand(), true)).'.'.$this-&gt;file-&gt;guessExtension();
          }
      }
  
      /**
       * @ORM\PostPersist()
       * @ORM\PostUpdate()
       */
      public function upload()
      {
          if (null === $this-&gt;file) {
              return;
          }
  
          // if there is an error when moving the file, an exception will
          // be automatically thrown by move(). This will properly prevent
          // the entity from being persisted to the database on error
          $this-&gt;file-&gt;move($this-&gt;getUploadRootDir(), $this-&gt;path);
  
          unset($this-&gt;file);
      }
  
      /**
       * @ORM\PostRemove()
       */
      public function removeUpload()
      {
          if ($file = $this-&gt;getAbsolutePath()) {
              unlink($file);
          }
      }    
  
      public function getAbsolutePath()
      {
          return null === $this-&gt;path ? null : $this-&gt;getUploadRootDir().'/'.$this-&gt;path;
      }
  
      public function getWebPath()
      {
          return null === $this-&gt;path ? null : $this-&gt;getUploadDir().'/'.$this-&gt;path;
      }
  
      protected function getUploadRootDir()
      {
          // the absolute directory path where uploaded documents should be saved
          return __DIR__.'/../../../../web/'.$this-&gt;getUploadDir();
  
      }
  
      protected function getUploadDir()
      {
          // get rid of the __DIR__ so it doesn't screw when displaying uploaded doc/image in the view.
          return 'uploads/images';
      }
  
  
      /**
       * Get id
       *
       * @return integer 
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * Set name
       *
       * @param string $name
       */
      public function setName($name)
      {
          $this-&gt;name = $name;
      }
  
      /**
       * Get name
       *
       * @return string 
       */
      public function getName()
      {
          return $this-&gt;name;
      }
  
      /**
       * Set width
       *
       * @param integer $width
       */
      public function setWidth($width)
      {
          $this-&gt;width = $width;
      }
  
      /**
       * Get width
       *
       * @return integer 
       */
      public function getWidth()
      {
          return $this-&gt;width;
      }
  
      /**
       * Set height
       *
       * @param integer $height
       */
      public function setHeight($height)
      {
          $this-&gt;height = $height;
      }
  
      /**
       * Get height
       *
       * @return integer 
       */
      public function getHeight()
      {
          return $this-&gt;height;
      }
  
      /**
       * Set path
       *
       * @param string $path
       */
      public function setPath($path)
      {
          $this-&gt;path = $path;
      }
  
      /**
       * Get path
       *
       * @return string 
       */
      public function getPath()
      {
          return $this-&gt;path;
      }
  
      /**
       * Set company
       *
       * @param BizTV\BackendBundle\Entity\company $company
       */
      public function setCompany(\BizTV\BackendBundle\Entity\company $company)
      {
          $this-&gt;company = $company;
      }
  
      /**
       * Get company
       *
       * @return BizTV\BackendBundle\Entity\company 
       */
      public function getCompany()
      {
          return $this-&gt;company;
      }
  
      /**
       * Set folder
       *
       * @param BizTV\MediaManagementBundle\Entity\Folder $folder
       */
      public function setFolder(\BizTV\MediaManagementBundle\Entity\Folder $folder = NULL)
      {
          $this-&gt;folder = $folder;
      }
  
      /**
       * Get folder
       *
       * @return BizTV\MediaManagementBundle\Entity\Folder 
       */
      public function getFolder()
      {
          return $this-&gt;folder;
      }
  
  
  }
  </code></pre>
  
  <p>And the form:</p>
  
  <pre><code>&lt;?php
  
  namespace BizTV\MediaManagementBundle\Form;
  
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilder;
  
  class ImageType extends AbstractType
  {
      function __construct($createAction=0) {
          $this-&gt;createAction = $createAction;    
      }   
  
      public function buildForm(FormBuilder $builder, array $options)
      {
  
          $createAction = $this-&gt;createAction;
  
          if ($createAction) {        
              $builder
                  -&gt;add('file')
              ;
          }
  
          $builder
              -&gt;add('name', 'text', array('label' =&gt; 'Namn'))
          ;
      }
  
      public function getName()
      {
          return 'biztv_mediamanagementbundle_imagetype';
      }
  }
  </code></pre>