title: File Upload doesn't create UploadedFile
body: <p>I am currently working on a PHP v5.5.9 project in which I am using some standalone components (I <strong>do not</strong> use the full-stack framework), currently the following:</p>
  
  <pre><code>psr/log                      1.0.0   Common interface for logging libraries
  sonata-project/intl-bundle   2.2.1   Symfony SonataIntlBundle
  symfony/debug                v2.4.2  Symfony Debug Component
  symfony/dependency-injection v2.4.2  Symfony DependencyInjection Component
  symfony/event-dispatcher     v2.4.2  Symfony EventDispatcher Component
  symfony/form                 v2.4.2  Symfony Form Component
  symfony/http-foundation      v2.4.2  Symfony HttpFoundation Component
  symfony/http-kernel          v2.4.2  Symfony HttpKernel Component
  symfony/icu                  v1.2.0  Contains an excerpt of the ICU data and classes to load it.
  symfony/intl                 v2.4.2  A PHP replacement layer for the C intl extension that includes additional data from the ICU library.
  symfony/locale               v2.4.2  Symfony Locale Component
  symfony/options-resolver     v2.4.2  Symfony OptionsResolver Component
  symfony/property-access      v2.4.2  Symfony PropertyAccess Component
  symfony/security-core        v2.4.2  Symfony Security Component - Core Library
  symfony/security-csrf        v2.4.2  Symfony Security Component - CSRF Library
  symfony/templating           v2.4.2  Symfony Templating Component
  symfony/translation          v2.4.2  Symfony Translation Component
  symfony/twig-bridge          v2.4.2  Symfony Twig Bridge
  symfony/validator            v2.4.2  Symfony Validator Component
  twig/extensions              v1.0.1  Common additional features for Twig that do not directly belong in core
  twig/twig                    v1.15.1 Twig, the flexible, fast, and secure template language for PHP
  </code></pre>
  
  <p>I have one <em>Entity</em> class, one <em>FormType</em> class and one <em>Controller</em> and I am able to persist the <em>Entity</em> via a "Twig" template <em>View</em>. So the full stack is working correctly.</p>
  
  <p>Then I added a <a href="http://api.symfony.com/2.4/Symfony/Component/HttpFoundation/File/File.html" rel="nofollow"><code>File</code></a> attribute to my <em>Entity</em>, in order to add an image file as described in the official documentation]<a href="http://symfony.com/doc/current/reference/constraints/File.html" rel="nofollow">1</a>. I've also extended my <em>FormType</em> with the following code:</p>
  
  <pre><code>$builder-&gt;add('pictureFile', 'file', array('label' =&gt; 'Image File'));
  </code></pre>
  
  <p>After running the updated code (with an image file selected), the following error is shown:</p>
  
  <pre><code>Catchable fatal error: Argument 1 passed to MyEntity::setPictureFile() must be an instance of Symfony\Component\HttpFoundation\File\File, array given, called in [.]\vendor\symfony\property-access\Symfony\Component\PropertyAccess\PropertyAccessor.php on line [.] and defined in MyEntity.php on line [.]
  </code></pre>
  
  <p>So, Symfony <strong>does not</strong> automatically create a <a href="http://api.symfony.com/2.4/Symfony/Component/HttpFoundation/File/UploadedFile.html" rel="nofollow"><code>UploadedFile</code></a> from the form, but binds the <code>$_FILE</code> data to an <code>array</code>. I've noticed, that other people do also have this problem:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/14960954/how-to-make-symfony2-request-object-to-pass-uploadedfile-object-to-entity-class">how to make symfony2 Request object to pass UploadedFile object to entity class instead of string</a></li>
  <li><a href="https://stackoverflow.com/questions/20667366/symfony2-multiple-file-upload">Symfony2 multiple file upload</a></li>
  </ul>
  
  <p>At the moment I am using the following code in my <em>Controller</em>, but I would like to use the auto-binding feature (because at the moment I cannot validate the uploaded file in the <em>Entity</em>).</p>
  
  <pre><code>$formType = new MyType;
  $entity = new MyEntity;
  $form = $this-&gt;formFactory-&gt;create($formType, $entity);
  $form-&gt;handleRequest();
  
  if (true === $form-&gt;isSubmitted()) {
      if (true === $form-&gt;isValid()) {
          // TODO Replace ugly workaround to partially solve the problem of the file upload.
          $request = Request::createFromGlobals();
          $file = $request-&gt;files-&gt;get('myentity')['pictureFile'];
  
          // Persist entity, upload file to server and redirect.
      }
  }
  
  // Load View template.
  </code></pre>
  
  <p><strong>Edit (2014-03-03):</strong> Here's the relevant code of the <em>Entity</em>:</p>
  
  <pre><code>&lt;?php
  use Symfony\Component\HttpFoundation\File\File;
  use Symfony\Component\Validator\Constraints as Assert;
  use Symfony\Component\Validator\Mapping\ClassMetadata;
  
  class MyEntity
  {
      private $id;
      private $name;
      private $picture;
  
      public function getId()
      {
          return $this-&gt;id;
      }
  
      public function setId($id)
      {
          $this-&gt;id = $id;
      }
  
      public function getName()
      {
          return $this-&gt;name;
      }
  
      public function setName($name)
      {
          $this-&gt;name = $name;
      }
  
      public function getPictureFile()
      {
          return $this-&gt;pictureFile;
      }
  
      public function setPictureFile(File $pictureFile = null)
      {
          $this-&gt;pictureFile = $pictureFile;
      }
  
      public static function loadValidatorMetadata(ClassMetadata $metadata)
      {
          $metadata-&gt;addPropertyConstraint(
              'name',
              new Assert\NotBlank
          );
          $metadata-&gt;addPropertyConstraint(
              'name',
              new Assert\Type('string'));
          $metadata-&gt;addPropertyConstraint(
              'pictureFile',
              new Assert\Image(
                  array(
                      'maxSize' =&gt; '2048k',
                      'minWidth' =&gt; 640,
                      'maxWidth' =&gt; 4096,
                      'minHeight' =&gt; 480,
                      'maxHeight' =&gt; 4096
                  )
              )
        );
      }
  }
  </code></pre>
  
  <p>And the source code of the <em>FormType</em>:</p>
  
  <pre><code>&lt;?php
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\OptionsResolver\OptionsResolverInterface;
  
  class MyType extends AbstractType
  {
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder-&gt;add('name', 'text')
              -&gt;add(
                  'pictureFile',
                  'file',
                  array('label' =&gt; 'Image File')
              )
              -&gt;add('submit', 'submit', array('label' =&gt; 'Save'));
      }
  
      public function getName()
      {
          return 'myentity';
      }
  
      public function setDefaultOptions(OptionsResolverInterface $resolver)
      {
          $resolver-&gt;setDefaults(array('data_class' =&gt; __NAMESPACE__ . '\MyEntity'));
      }
  }
  </code></pre>
  
  <p>Any suggestions how-to get this work?</p>
  
  <p><strong>Edit (2014-03-07):</strong> Simplified source code in preparation for my answer.</p>