title: Serialization of 'Symfony\Component\HttpFoundation\File\UploadedFile' is not allowed
body: <p>This error occurs when I try to upload a bad file which does not match with the image assert. Only image are accepted.</p>
  
  <p>user entity :</p>
  
  <pre><code>&lt;?php
  
  namespace Test\BackBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  use Symfony\Component\Security\Core\User\UserInterface;
  use Symfony\Component\Validator\Constraints as Assert;
  use Symfony\Component\HttpFoundation\File\UploadedFile;
  use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  
  /**
   * User
   *
   * @ORM\Table(name="user")
   * @ORM\Entity
   * @UniqueEntity(
   *     fields={"email"},
   *     message="This email already exists."
   *  )
   */
  class User implements UserInterface
  {
      /**
       * @var integer
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
      /**
       * @var string
       *
       * @ORM\Column(name="lastName", type="string", length=255)
       * @Assert\NotBlank()
       */
      private $lastName;
  
      /**
       * @var string
       *
       * @ORM\Column(name="firstName", type="string", length=255)
       * @Assert\NotBlank()
       */
      private $firstName;
  
      /**
       * @var string
       *
       * @ORM\Column(name="job", type="string", length=255, nullable=true)
       */
      private $job;
  
      /**
       * @var string
       *
       * @ORM\Column(name="email", type="string", length=255, unique=true)
       * @Assert\Email()
       */
      private $email;
  
      /**
       * @var string
       *
       * @ORM\Column(name="password", type="string", length=255)
       * @Assert\NotBlank()
       */
      private $password;
  
      /**
       * @var string
       *
       * @ORM\Column(name="salt", type="string", length=255)
       */
      private $salt;
  
      /**
       * @var array
       *
       * @ORM\Column(name="roles", type="array")
       * @Assert\NotBlank()
       */
      private $roles;
  
      /**
       * @var boolean
       *
       * @ORM\Column(name="isActive", type="boolean")
       */
      private $isActive;
  
      /**
       * @var string
       *
       * @ORM\Column(name="avatar", type="string", length=255, nullable=true)
       */
      private $path;
  
      /**
       * @var string
       *
       * @Assert\Image()
       */
      public $file;
  
      public function __construct()
      {
          $this-&gt;isActive = true;
          $this-&gt;salt = md5(uniqid(null, true));
      }
  
      /**
       * Get id
       *
       * @return integer 
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * Set lastName
       *
       * @param string $lastName
       * @return User
       */
      public function setLastName($lastName)
      {
          $this-&gt;lastName = $lastName;
  
          return $this;
      }
  
      /**
       * Get lastName
       *
       * @return string 
       */
      public function getLastName()
      {
          return $this-&gt;lastName;
      }
  
      /**
       * Set firstName
       *
       * @param string $firstName
       * @return User
       */
      public function setFirstName($firstName)
      {
          $this-&gt;firstName = $firstName;
  
          return $this;
      }
  
      /**
       * Get firstName
       *
       * @return string 
       */
      public function getFirstName()
      {
          return $this-&gt;firstName;
      }
  
      /**
       * Set job
       *
       * @param string $job
       * @return User
       */
      public function setJob($job)
      {
          $this-&gt;job = $job;
  
          return $this;
      }
  
      /**
       * Get job
       *
       * @return string 
       */
      public function getJob()
      {
          return $this-&gt;job;
      }
  
      /**
       * Set email
       *
       * @param string $email
       * @return User
       */
      public function setEmail($email)
      {
          $this-&gt;email = $email;
  
          return $this;
      }
  
      /**
       * Get email
       *
       * @return string 
       */
      public function getEmail()
      {
          return $this-&gt;email;
      }
  
      /**
       * Set password
       *
       * @param string $password
       * @return User
       */
      public function setPassword($password)
      {
          $this-&gt;password = $password;
  
          return $this;
      }
  
      /**
       * Get password
       *
       * @return string 
       */
      public function getPassword()
      {
          return $this-&gt;password;
      }
  
      /**
       * Set salt
       *
       * @param string $salt
       * @return User
       */
      public function setSalt($salt)
      {
          $this-&gt;salt = $salt;
  
          return $this;
      }
  
      /**
       * Get salt
       *
       * @return string 
       */
      public function getSalt()
      {
          return $this-&gt;salt;
      }
  
      /**
       * Set role
       *
       * @param array $role
       * @throws \InvalidArgumentException
       * @return User
       */
      public function setRoles($role)
      {
          if(array_diff($role, array("ROLE_SUPER_ADMIN", "ROLE_ADMIN", "ROLE_CUSTOMER"))) {
              throw new \InvalidArgumentException("Bad role");
          }
          $this-&gt;roles = $role;
  
          return $this;
      }
  
      /**
       * Get role
       *
       * @return array 
       */
      public function getRoles()
      {
          return $this-&gt;roles;
      }
  
      /**
       * Set isActive
       *
       * @param boolean $isActive
       * @return User
       */
      public function setIsActive($isActive)
      {
          $this-&gt;isActive = $isActive;
  
          return $this;
      }
  
      /**
       * Get isActive
       *
       * @return boolean 
       */
      public function getIsActive()
      {
          return $this-&gt;isActive;
      }
  
      /**
       * @inheritDoc
       */
      public function eraseCredentials()
      {
      }
  
      /**
       * Set username
       *
       * @param string $email
       *
       * @return string
       */
      public function setUsername($email)
      {
          $this-&gt;email = $email;
  
          return $this;
      }
  
      /**
       * Get username
       *
       * @return string
       */
      public function getUsername()
      {
          return $this-&gt;email;
      }
  
      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()
      {
          return __DIR__.'/../../../../web/'.$this-&gt;getUploadDir();
      }
  
      protected function getUploadDir()
      {
          return 'uploads/img';
      }
  
      public function upload()
      {
          if (null === $this-&gt;file) {
              return;
          } else {
              $this-&gt;path = $this-&gt;firstName.'_'.$this-&gt;lastName.'_'.sha1(uniqid(mt_rand(), true)).'.'.$this-&gt;file-&gt;guessExtension();
          }
  
          $this-&gt;file-&gt;move($this-&gt;getUploadRootDir(), $this-&gt;path);
  
          $this-&gt;file = null;
      }
  
      public function getPath()
      {
          return $this-&gt;getWebPath();
      }
  }
  </code></pre>
  
  <p>userType:</p>
  
  <pre><code>$builder
      -&gt;add('firstName', 'text', array(
                      'required' =&gt; true
                  ))
       -&gt;add('lastName', 'text', array(
                      'required' =&gt; true
                  ))
       -&gt;add('email', 'email', array(
                      'required' =&gt; true
                  ))
       -&gt;add('job', 'text', array(
                      'required' =&gt; false
                  ))
       -&gt;add('file', 'file', array(
                      'label' =&gt; false,
                      'required' =&gt; false,
                  ))
              ;
  </code></pre>
  
  <p>controller : </p>
  
  <pre><code>    public function updateMyAccountAction($id, Request $request)
      {
          $entityManager = $this-&gt;get('doctrine')-&gt;getManager();
  
          $user = $this-&gt;get('doctrine')
              -&gt;getRepository('TestBackBundle:User')
              -&gt;find($id);
  
          if (!$user) {
              throw $this-&gt;createNotFoundException('Unable to find User entity.');
          }
  
          $editForm = $this-&gt;createForm(new UserType(), $user);
  
          $editForm-&gt;handleRequest($request);
  
          if ($editForm-&gt;isValid()) {
  
              $user-&gt;upload();
  
              $entityManager-&gt;persist($user);
              $entityManager-&gt;flush();
              $this-&gt;get('session')-&gt;getFlashBag()-&gt;add('success', 'Your profile has been updated');
  
              return $this-&gt;redirect($this-&gt;generateUrl('my_account', array('id' =&gt; $id)));
          } else {
              $this-&gt;get('session')-&gt;getFlashBag()-&gt;add('error', 'Erreur');
  
              return $this-&gt;redirect($this-&gt;generateUrl('my_account', array('id' =&gt; $id)));
          }
      }
  </code></pre>
  
  <p>When I try to test if the image assert works updating for example a pdf file, this error occurs. The file is not updated so it is good. But my flash bag and redirection in my controller don't work... if I write <code>var_dump("test")</code> in the else in my controller <code>"test"</code> is displayed and the error too so Symfony detects that the form is not valid.</p>
  
  <p>This is a part of the Stack Trace when error occurs :</p>
  
  <blockquote>
    <p>in
    /home/user/www/sf2/vendor/symfony/symfony/src/Symfony/Component/Security/Core/Authentication/Token/AbstractToken.php
    at line 155</p>
  </blockquote>
  
  <pre><code>152.                $this-&gt;roles,
  153.                $this-&gt;attributes
  154.            )
  155.        );
      }
      /**
  </code></pre>
  
  <blockquote>
    <p>at serialize (array(object(User), true, array(object(Role)), array()))
    in
    /home/kevin/www/sf2/vendor/symfony/symfony/src/Symfony/Component/Security/Core/Authentication/Token/AbstractToken.php
    at line 155</p>
  </blockquote>
  
  <p>Reading it, I feel that there is a problem with the roles attribute to serialize it because it is an array (we have to declare this attribute as an array implementing <code>UserInterface</code>)</p>
  
  <p>So why this error occurs ? </p>