title: Sonata admin file uploads : Upload files apart from the admin panel
body: <p>I have a Symfony 2.8 based project, I have Sonata admin bundle and Sonata user bundle well installed and all is working good.</p>
  
  <p>I have an "Image" entity that it meant to contain my uploaded file. I followed the official Sonata tutorial to how to upload a file (<a href="https://sonata-project.org/bundles/admin/master/doc/cookbook/recipe_file_uploads.html" rel="nofollow">https://sonata-project.org/bundles/admin/master/doc/cookbook/recipe_file_uploads.html</a>) and all is working perfectly when I want to upload a file from the admin panel.</p>
  
  <p>Now, I need a to offer the possibility to a simple connected user (not admin) to upload a file as well from a form.</p>
  
  <p>Here's the example I have:</p>
  
  <p>I have this "Offer" class that has an "Image" attribute:</p>
  
  <pre><code>class Offer {
  
      /**
       * @var int
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
      /**
       * @var string
       *
       * @ORM\Column(name="title", type="string", length=255)
       */
      private $title;
  
      /**
       * @var string
       *
       * @ORM\Column(name="body", type="text")
       */
      private $body;
  
      /**
       * @var \DateTime
       *
       * @ORM\Column(name="date", type="datetime")
       */
      private $date;
  
      /**
       * @ORM\OneToOne(targetEntity="AIEM\PlatformBundle\Entity\Image", cascade={"all"})
       */
      private $image;
  
     //Getters and Setters
  }
  </code></pre>
  
  <p>Before adding the "Image" entity, I persisted an offer using the classic way : Getting my data from the request (example : <code>$offer-&gt;setTitre($request-&gt;request-&gt;get('title'));</code>). But now since I have a OneToMany with a file, I don't know how to proceed.</p>
  
  <p>I'll be grateful if you can share some ideas.</p>
  
  <p><strong>EDIT</strong>
  Here's my OfferAdmin, and it's working perfectly in sonata admin</p>
  
  <pre><code>class OfferAdmin extends Admin {
  
      protected function configureFormFields(FormMapper $formMapper) {
          $formMapper-&gt;add('title', 'text')
                  -&gt;add('image', 'sonata_type_admin')
                  -&gt;add('body', 'textarea', array("attr" =&gt; array("class" =&gt; "ckeditor")));
      }
  
      protected function configureDatagridFilters(DatagridMapper $datagridMapper) {
          $datagridMapper-&gt;add('titre')
                  -&gt;add('date');
      }
  
      protected function configureListFields(ListMapper $listMapper) {
          $listMapper-&gt;addIdentifier('titre');
      }
  
      public function prePersist($page) {
          $this-&gt;manageEmbeddedImageAdmins($page);
      }
  
      public function preUpdate($page) {
          $this-&gt;manageEmbeddedImageAdmins($page);
      }
  
      private function manageEmbeddedImageAdmins($page) {
  
          /** @var Image $image */
          $image = $page-&gt;getImage();
  
          if ($image) {
              if ($image-&gt;getFile()) {
                  // update the Image to trigger file management
                  $image-&gt;refreshUpdated();
              } elseif (!$image-&gt;getFile() &amp;&amp; !$image-&gt;getFilename()) {
                  // prevent Sf/Sonata trying to create and persist an empty Image
                  $page-&gt;$setImage(null);
              }
          }
      }
  
  }
  </code></pre>
  
  <p>And the form I want the user to add the file from looks like this</p>
  
  <pre><code>&lt;form method="post" action="{{ path('aiem_platform_add_offer')}}"&gt;
      &lt;div class="form-group"&gt;
          &lt;label for="title"&gt;Title&lt;/label&gt;
          &lt;input type="text" class="form-control" id="title" name="title" placeholder="Title"&gt;
      &lt;/div&gt;
  
  
      &lt;div class="form-group"&gt;
          &lt;input type="file" class="form-control" id="myFile" name="myFile"&gt;
      &lt;/div&gt;
  
      &lt;div class="form-group"&gt;
          &lt;label for="contenu"&gt;Contenu&lt;/label&gt;
          &lt;textarea class="form-control ckeditor" id="body" name="body"&gt;&lt;/textarea&gt;
      &lt;/div&gt;
  
      &lt;button type="submit" class="btn btn-default"&gt;Add&lt;/button&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Thank you</p>