title: Validating a non-mapped field in a Symfony2 form class when all other fields are mapped to an entity
body: <p>I have a form in my Symfony2 application that is largely used for persisting an entity, but I have added one extra non-mapped field that is used for uploading a file that is then processed and deleted. However, I can't figure out how to validate this additional field.</p>
  
  <p>Here it is defined in the <code>buildForm()</code> method of my form class:</p>
  
  <pre><code>public function buildForm(FormBuilderInterface $builder, array $options)
  {
      $builder
          -&gt;add('name', 'text', array('label' =&gt; 'Name'))
          // ...
          -&gt;add('data', 'file', array(
              'label'    =&gt; 'CSV Data File',
              'mapped'   =&gt; false,
              'required' =&gt; false,
          ));
  }
  </code></pre>
  
  <p>I initially tried adding the validation for this field to the bundle's <code>validation.yml</code> file like so, but as this field is not part of the entity, it threw up an error saying so.</p>
  
  <pre class="lang-none prettyprint-override"><code>My\Bundle\Entity\MyEntity:
    properties:
      data:
        - File:
            maxSize: 1024k
            mimeTypes: text/*
            mimeTypesMessage: Please upload a CSV file
  </code></pre>
  
  <p>My next attempt was to add the constraints within the form class itself, like so, but it appears you can only validate arrays this way, not objects, which I guess is because it's mainly tied to my entity.</p>
  
  <pre><code>public function setDefaultOptions(OptionsResolverInterface $resolver)
  {
      $collectionConstraint = new Collection(array(
          'data' =&gt; new File(array(
              'maxSize'          =&gt; '1024k',
              'mimeTypes'        =&gt; 'text/*',
              'mimeTypesMessage' =&gt; 'Please upload a CSV file',
          )),
      ));
  
      $resolver-&gt;setDefaults(array(
          'data_class'  =&gt; 'My\Bundle\Entity\MyEntity',
          'constraints' =&gt; $collectionConstraint,
      ));
  }
  </code></pre>
  
  <p>From the documentation, it looks like I can use <code>$this-&gt;get('validator')-&gt;validateValue();</code> to validate the extra field on its own, but I'd quite like to validate the whole form at once and display any errors relating to the non-mapped field with the field itself.</p>
  
  <p>Does anyone have any ideas?</p>