title: VichUploader image upload in embedded form (Symfony 3)
body: <p><strong>How can I successfully embed a running image upload form inside a classified form with OneToOne relationship ?</strong></p>
  
  <p><em>first post ever on Stackoverflow so please be gentle (and I'm french ...^^)</em></p>
  
  <p>I have a Symfony 3.3 project, running great. I use VichUploaderBundle for upload and LiipImagine for rendering. Currently, NO PROBLEM for a single image File upload !
  I have an uploadtest page, I show one image file upload form, I upload the file ... it goes throught the controller and everything is fine. File uploaded and moved in directory, page shows new image, with all filters. Great !</p>
  
  <p><strong><em>My real problem comes when embedding the form inside another form</em></strong>.</p>
  
  <p>I would like to embed my "image form" inside a classified form. I started by creating the field <em>mainImage</em> inside this classified form. The relation from the classified is OneToOne, and of course it has cascade="persist" &amp; "remove".</p>
  
  <p>But on validation, nothing is triggered and I got the following error on the image_name :</p>
  
  <blockquote>
    <p>--- An exception occurred while executing 'INSERT INTO vehicle_ad (createdAt, updatedAt, published, publishedAt, friendsPriority,
    friendsPriorityStart, purchaseDate, sellPrice, currency, odometerType,
    currentOdometer, energy, gearbox, mainColor, nickname, description,
    creator_id, vehicle_brand_id, vehicle_model_id, main_image_id,
    vehicle_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
    ?, ?, ?, ?)' with params ["2018-01-23 17:15:39", "2018-01-23
    17:15:39", 0, null, 0, null, null, 2650, "eur", "kilometers", 17340,
    null, null, null, null, null, 1, 3, 1, null, null]:</p>
    
    <p>SQLSTATE[23000]: Integrity constraint violation: 1048 Le champ</p>
    
    <h2>'main_image_id' ne peut être vide (null)</h2>
  </blockquote>
  
  <p>File is empty, vichuploader namer not triggered ... and I am stuck with it since a few days. I did a lot of research ... so any help will be greatly appreciated.</p>
  
  <p>I try to give my code as clearly as possible. First, what is OK (single image upload) :</p>
  
  <blockquote>
    <blockquote>
      <blockquote>
        <p>Image class</p>
      </blockquote>
    </blockquote>
  </blockquote>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?php
   
  namespace Gac\MediasBundle\Entity;
   
  use Doctrine\ORM\Mapping as ORM;
  use Symfony\Component\Validator\Constraints as Assert;
  use Symfony\Component\HttpFoundation\File\File;
  use Vich\UploaderBundle\Mapping\Annotation as Vich;
   
  /**
   * Image
   *
   * @ORM\Table(name="image")
   * @ORM\Entity(repositoryClass="Gac\MediasBundle\Repository\ImageRepository")
   * @Vich\Uploadable
   */
  class Image
  {
      /**
       * @var int
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
       
      /* ----------------------------------------------------------------------
      ---------   Connection with other entities
      ------------------------------------------------------------------------- */
       
      /**
       * @ORM\ManyToOne(targetEntity="Gac\UserBundle\Entity\User", inversedBy="images")
       * @ORM\JoinColumn(nullable=false)
       */
      private $creator;
       
      /**
       * @ORM\ManyToOne(targetEntity="Gac\AdsBundle\Entity\VehicleAd", inversedBy="images")
       * @ORM\JoinColumn(nullable=true)
       */
      private $vehicleAd;
       
      // -------------------------------------------------------------------------------
   
      /**
       * @var string
       *
       * @ORM\Column(name="imageName", type="string", length=255)
       */
      private $imageName;
   
      /**
       * @Vich\UploadableField(mapping="users_images", fileNameProperty="imageName")
       * @var File
       */
      private $imageFile;
   
      /**
       * @var \DateTime
       *
       * @ORM\Column(name="createdAt", type="datetime")
       * @Assert\DateTime()
       */
      private $createdAt;
   
      /**
       * @var \DateTime
       *
       * @ORM\Column(name="updatedAt", type="datetime")
       * @Assert\DateTime()
       */
      private $updatedAt;
   
      /**
       * @var \DateTime
       *
       * @ORM\Column(name="hiddenAt", type="datetime", nullable=true)
       * @Assert\DateTime()
       */
      private $hiddenAt;
       
       
      public function __construct($thisuser)
      {
          $this-&gt;creator = $thisuser;
          $this-&gt;createdAt = new \Datetime();
          $this-&gt;updatedAt = new \Datetime();
      }
       
      /**
       * Get id
       *
       * @return int
       */
      public function getId()
      {
          return $this-&gt;id;
      }
       
      /**
       * Set creator
       *
       * @param \Gac\UserBundle\Entity\User $creator
       *
       * @return Image
       */
      public function setCreator(\Gac\UserBundle\Entity\User $creator)
      {
          $this-&gt;creator = $creator;
   
          return $this;
      }
   
      /**
       * Get creator
       *
       * @return \Gac\UserBundle\Entity\User
       */
      public function getCreator()
      {
          return $this-&gt;creator;
      }
   
      /**
       * Set imageName
       *
       * @param string $imageName
       *
       * @return Image
       */
      public function setImageName($imageName)
      {
          $this-&gt;imageName = $imageName;
   
          return $this;
      }
   
      /**
       * Get imageName
       *
       * @return string
       */
      public function getImageName()
      {
          return $this-&gt;imageName;
      }
       
      public function setImageFile(File $image = null)
      {
          $this-&gt;imageFile = $image;
   
          // VERY IMPORTANT:
          // It is required that at least one field changes if you are using Doctrine,
          // otherwise the event listeners won't be called and the file is lost
          if ($image) {
              $this-&gt;updatedAt = new \DateTime();
          }
      }
       
      /**
       * Get imageFile
       */
      public function getImageFile()
      {
          return $this-&gt;imageFile;
      }
   
      /**
       * Set createdAt
       *
       * @param \DateTime $createdAt
       *
       * @return Image
       */
      public function setCreatedAt($createdAt)
      {
          $this-&gt;createdAt = $createdAt;
   
          return $this;
      }
   
      /**
       * Get createdAt
       *
       * @return \DateTime
       */
      public function getCreatedAt()
      {
          return $this-&gt;createdAt;
      }
   
      /**
       * Set updatedAt
       *
       * @param \DateTime $updatedAt
       *
       * @return Image
       */
      public function setUpdatedAt($updatedAt)
      {
          $this-&gt;updatedAt = $updatedAt;
   
          return $this;
      }
   
      /**
       * Get updatedAt
       *
       * @return \DateTime
       */
      public function getUpdatedAt()
      {
          return $this-&gt;updatedAt;
      }
   
      /**
       * Set hiddenAt
       *
       * @param \DateTime $hiddenAt
       *
       * @return Image
       */
      public function setHiddenAt($hiddenAt)
      {
          $this-&gt;hiddenAt = $hiddenAt;
   
          return $this;
      }
   
      /**
       * Get hiddenAt
       *
       * @return \DateTime
       */
      public function getHiddenAt()
      {
          return $this-&gt;hiddenAt;
      }
   
      /**
       * Set vehicleAd
       *
       * @param \Gac\AdsBundle\Entity\VehicleAd $vehicleAd
       *
       * @return Image
       */
      public function setVehicleAd(\Gac\AdsBundle\Entity\VehicleAd $vehicleAd = null)
      {
          $this-&gt;vehicleAd = $vehicleAd;
   
          return $this;
      }
   
      /**
       * Get vehicleAd
       *
       * @return \Gac\AdsBundle\Entity\VehicleAd
       */
      public function getVehicleAd()
      {
          return $this-&gt;vehicleAd;
      }
  }</code></pre>
  </div>
  </div>
  </p>
  
  <blockquote>
    <blockquote>
      <blockquote>
        <p>Then the related IMAGE FORM :</p>
      </blockquote>
    </blockquote>
  </blockquote>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?php
   
  namespace Gac\MediasBundle\Form;
   
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\OptionsResolver\OptionsResolver;
  use Vich\UploaderBundle\Form\Type\VichImageType;
   
  class ImageType extends AbstractType
  {
      /**
       * {@inheritdoc}
       */
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder-&gt;add('imageFile',       VichImageType::class, [
                      'label' =&gt; false,
                      'required' =&gt; false,
                      'allow_delete' =&gt; false,
                  ]);
      }
       
      /**
       * {@inheritdoc}
       */
      public function configureOptions(OptionsResolver $resolver)
      {
          $resolver-&gt;setDefaults(array(
              'data_class' =&gt; 'Gac\MediasBundle\Entity\Image'
          ));
      }
   
      /**
       * {@inheritdoc}
       */
      public function getBlockPrefix()
      {
          //return 'gac_mediasbundle_image';
          return 'ImageType';
      }
   
   
  }</code></pre>
  </div>
  </div>
  </p>
  
  <blockquote>
    <blockquote>
      <blockquote>
        <p>And the controller</p>
      </blockquote>
    </blockquote>
  </blockquote>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>{% extends 'medias/layout.html.twig' %}
   
  {% block bundle_content %}
   
  &lt;div class="jumbotron text-center"&gt;
      &lt;h1&gt;Testing user's images UPLOAD&lt;/h1&gt;
  &lt;/div&gt;
   
  &lt;p&gt;-----------------&lt;/p&gt;
  {{ form_start(form) }}
  {{ form_errors(form) }}
  {{ form_row(form) }}
  &lt;button type="submit"&gt;Upload&lt;/button&gt;
  {{ form_end(form) }}
  &lt;p&gt;-----------------&lt;/p&gt;
   
  {% for picture in pictures %}
      &lt;div class="row"&gt;
          &lt;div class="media"&gt;
              &lt;div class="media-left media-middle"&gt;
                  &lt;img class="media-object" src="{{ picture.imageName | imagine_filter('tiny_test') }}" alt="{{ 'GearsAndCoffee-'~picture.imageName }}" /&gt;&lt;br/&gt;
              &lt;/div&gt;
              &lt;div class="media-body"&gt;
                  &lt;h4 class="media-heading"&gt;Filter name :&lt;/h4&gt;
                  &lt;em&gt;tiny_test&lt;/em&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="media"&gt;
              &lt;div class="media-left media-middle"&gt;
                  &lt;a href="{{ asset('uploads/images/'~picture.imageName) }}" /&gt;
                      &lt;img class="media-object" src="{{ picture.imageName | imagine_filter('std_display') }}" alt="{{ 'GearsAndCoffee-'~picture.imageName }}" /&gt;&lt;br/&gt;
                  &lt;/a&gt;
              &lt;/div&gt;
              &lt;div class="media-body"&gt;
                  &lt;h4 class="media-heading"&gt;Filter name :&lt;/h4&gt;
                  &lt;em&gt;std_display&lt;/em&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;p&gt;
              &lt;a href="{{ path('gac_medias_updatetest', {'id': picture.id}) }}"&gt;&lt;strong&gt;UPDATE IMAGE&lt;/strong&gt;&lt;/a&gt;
          &lt;/p&gt;
          &lt;p&gt;
              &lt;a href="{{ path('gac_medias_removetest', {'id': picture.id}) }}"&gt;&lt;strong&gt;DELETE IMAGE&lt;/strong&gt;&lt;/a&gt;
          &lt;/p&gt;
      &lt;/div&gt;
      &lt;p&gt;-----------------&lt;/p&gt;
  {% endfor %}
   
  {% endblock bundle_content %}</code></pre>
  </div>
  </div>
  </p>
  
  <blockquote>
    <blockquote>
      <blockquote>
        <p>As an information, the VichUploader Bundle config :</p>
      </blockquote>
    </blockquote>
  </blockquote>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>vich_uploader:
      db_driver: orm
      #templating: true
      #twig:       true               
      #form:       true
      mappings:
          users_images:
              uri_prefix:         '%app.path.users_images%'
              upload_destination: '%kernel.root_dir%/../web/uploads/images'
              namer:              vich_uploader.namer_uniqid
              inject_on_load:     true
              delete_on_update:   true
              delete_on_remove:   true</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong><em>And now the code of what is NOT RUNNING on my app :</em></strong></p>
  
  <blockquote>
    <blockquote>
      <blockquote>
        <p>The classified class</p>
      </blockquote>
    </blockquote>
  </blockquote>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?php
   
  namespace Gac\AdsBundle\Entity;
   
  use Doctrine\ORM\Mapping as ORM;
  use Symfony\Component\Validator\Constraints as Assert;
  use Doctrine\Common\Collections\ArrayCollection;
   
  /**
   * VehicleAd
   *
   * @ORM\Table(name="vehicle_ad")
   * @ORM\Entity(repositoryClass="Gac\AdsBundle\Repository\VehicleAdRepository")
   * @ORM\HasLifecycleCallbacks
   */
  class VehicleAd
  {
      /**
       * @var int
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
       
      /* ----------------------------------------------------------------------
      ---------   Connection with other entities
      ------------------------------------------------------------------------- */
       
      /**
       * @ORM\ManyToOne(targetEntity="Gac\UserBundle\Entity\User")
       * @ORM\JoinColumn(nullable=false)
       */
      private $creator;
       
      /**
       * @ORM\ManyToOne(targetEntity="Gac\VehiclesBundle\Entity\VehicleBrand")
       * @ORM\JoinColumn(nullable=false)
       */
      private $vehicleBrand;
       
      /**
       * @ORM\ManyToOne(targetEntity="Gac\VehiclesBundle\Entity\VehicleModel")
       * @ORM\JoinColumn(nullable=false)
       */
      private $vehicleModel;
       
      /**
       * @ORM\OneToOne(targetEntity="Gac\MediasBundle\Entity\Image", cascade={"persist", "remove"})
       * @ORM\JoinColumn(nullable=false)
       */
      private $mainImage;
       
      // -------------------------------------------------------------------------------
   
      // ---------------- General status and dates for the AD -------------
       
      /**
       * @var \DateTime
       *
       * @ORM\Column(name="createdAt", type="datetime")
       * @Assert\DateTime()
       */
      private $createdAt;
   
      /**
       * @var \DateTime
       *
       * @ORM\Column(name="updatedAt", type="datetime")
       * @Assert\DateTime()
       */
      private $updatedAt;
       
      // ---------------------- AD main content -----------------------
       
      /**
       * @var string
       *
       * @ORM\Column(name="description", type="text", nullable=true)
       * @Assert\Type("string")
       */
      private $description;
       
       
      // ************************* FUNCTIONS ***************************
       
      public function __construct($thisuser)
      {
          $this-&gt;creator = $thisuser;
          $this-&gt;createdAt = new \Datetime();
          $this-&gt;updatedAt = new \Datetime();
      }
       
      /**
       * Change updatedAt field just before ORM update
       *
       * @ORM\PreUpdate
       */
      public function changeUpdatedAt()
      {
          $this-&gt;updatedAt = new \Datetime();
      }
       
      /**
       * Get id
       *
       * @return int
       */
      public function getId()
      {
          return $this-&gt;id;
      }
   
      /**
       * Set createdAt
       *
       * @param \DateTime $createdAt
       *
       * @return VehicleAd
       */
      public function setCreatedAt($createdAt)
      {
          $this-&gt;createdAt = $createdAt;
   
          return $this;
      }
   
      /**
       * Get createdAt
       *
       * @return \DateTime
       */
      public function getCreatedAt()
      {
          return $this-&gt;createdAt;
      }
   
      /**
       * Set updatedAt
       *
       * @param \DateTime $updatedAt
       *
       * @return VehicleAd
       */
      public function setUpdatedAt($updatedAt)
      {
          $this-&gt;updatedAt = $updatedAt;
   
          return $this;
      }
   
      /**
       * Get updatedAt
       *
       * @return \DateTime
       */
      public function getUpdatedAt()
      {
          return $this-&gt;updatedAt;
      }
   
      /**
       * Set creator
       *
       * @param \Gac\UserBundle\Entity\User $creator
       *
       * @return VehicleAd
       */
      public function setCreator(\Gac\UserBundle\Entity\User $creator)
      {
          $this-&gt;creator = $creator;
   
          return $this;
      }
   
      /**
       * Get creator
       *
       * @return \Gac\UserBundle\Entity\User
       */
      public function getCreator()
      {
          return $this-&gt;creator;
      }
   
      /**
       * Set description
       *
       * @param string $description
       *
       * @return VehicleAd
       */
      public function setDescription($description)
      {
          $this-&gt;description = $description;
   
          return $this;
      }
   
      /**
       * Get description
       *
       * @return string
       */
      public function getDescription()
      {
          return $this-&gt;description;
      }
       
      /**
       * Set vehicleBrand
       *
       * @param \Gac\VehiclesBundle\Entity\VehicleBrand $vehicleBrand
       *
       * @return VehicleAd
       */
      public function setVehicleBrand(\Gac\VehiclesBundle\Entity\VehicleBrand $vehicleBrand)
      {
          $this-&gt;vehicleBrand = $vehicleBrand;
           
          return $this;
      }
   
      /**
       * Get vehicleBrand
       *
       * @return \Gac\VehiclesBundle\Entity\VehicleBrand
       */
      public function getVehicleBrand()
      {
          return $this-&gt;vehicleBrand;
      }
       
      /**
       * Set vehicleModel
       *
       * @param \Gac\VehiclesBundle\Entity\VehicleModel $vehicleModel
       *
       * @return VehicleAd
       */
      public function setVehicleModel(\Gac\VehiclesBundle\Entity\VehicleModel $vehicleModel)
      {
          $linkedBrand = $vehicleModel-&gt;getModelBrand();
           
          $this-&gt;vehicleBrand = $linkedBrand;
          $this-&gt;vehicleModel = $vehicleModel;
   
          return $this;
      }
   
      /**
       * Get vehicleModel
       *
       * @return \Gac\VehiclesBundle\Entity\VehicleModel
       */
      public function getVehicleModel()
      {
          return $this-&gt;vehicleModel;
      }
   
      /**
       * Set mainImage
       *
       * @param \Gac\MediasBundle\Entity\Image $mainImage
       *
       * @return VehicleAd
       */
      public function setMainImage(\Gac\MediasBundle\Entity\Image $mainImage)
      {
          $this-&gt;mainImage = $mainImage;
   
          return $this;
      }
   
      /**
       * Get mainImage
       *
       * @return \Gac\MediasBundle\Entity\Image
       */
      public function getMainImage()
      {
          return $this-&gt;mainImage;
      }
  }</code></pre>
  </div>
  </div>
  </p>
  
  <blockquote>
    <blockquote>
      <blockquote>
        <p>Of course the big one, my Classified Form !</p>
      </blockquote>
    </blockquote>
  </blockquote>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?php
   
  namespace Gac\AdsBundle\Form;
   
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormInterface;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\Form\FormEvent;
  use Symfony\Component\Form\FormEvents;
  use Symfony\Component\OptionsResolver\OptionsResolver;
  use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  use Symfony\Component\Form\Extension\Core\Type\TextType;
  use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  use Symfony\Component\Form\Extension\Core\Type\IntegerType;
  use Symfony\Component\Form\Extension\Core\Type\DateType;
  use Symfony\Component\Form\Extension\Core\Type\CollectionType;
  use Gac\MediasBundle\Form\ImageType;
  use Gac\VehiclesBundle\Entity\VehicleBrand;
  use Gac\VehiclesBundle\Entity\VehicleModel;
   
  class VehicleAdType extends AbstractType
  {
      /**
       * {@inheritdoc}
       */
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $globalDatas = $options['globalDatas'];
           
          $builder-&gt;add('vehicleBrand', EntityType::class, array(
                              'class' =&gt; 'GacVehiclesBundle:VehicleBrand',
                              'choice_label' =&gt; 'brandName',
                              'placeholder' =&gt; 'Please select a brand :',
                          ))
                  -&gt;add('vehicleModel', EntityType::class, array(
                              'class' =&gt; 'GacVehiclesBundle:VehicleModel',
                              'choice_label' =&gt; 'modelName',
                          ))
                  -&gt;add('description', TextareaType::class, array('required' =&gt; false));
           
          $builder-&gt;add('mainImage', ImageType::class);
           
          $formModifier = function (FormInterface $form, VehicleBrand $vehicleBrand = null) {
              $models = null === $vehicleBrand ? array() : $vehicleBrand-&gt;getVehicleModels();
   
              $form-&gt;add('vehicleModel', EntityType::class, array(
                              'class' =&gt; 'GacVehiclesBundle:VehicleModel',
                              'choice_label' =&gt; 'modelName',
                              'choices' =&gt; $models,
                          ));
          };
           
          $builder-&gt;addEventListener(
              FormEvents::PRE_SET_DATA,
              function (FormEvent $event) use ($formModifier) {
                  // this would be your entity, i.e. Brand
                  $data = $event-&gt;getData();
                   
                  $formModifier($event-&gt;getForm(), $data-&gt;getVehicleBrand());
              }
          );
           
          $builder-&gt;get('vehicleBrand')-&gt;addEventListener(
              FormEvents::POST_SUBMIT,
              function (FormEvent $event) use ($formModifier) {
                  $vehicleBrand = $event-&gt;getForm()-&gt;getData();
                  $formModifier($event-&gt;getForm()-&gt;getParent(), $vehicleBrand);
              }
          );
      }
       
      /**
       * {@inheritdoc}
       */
      public function configureOptions(OptionsResolver $resolver)
      {
          $resolver-&gt;setDefaults(array(
              'data_class' =&gt; 'Gac\AdsBundle\Entity\VehicleAd',
              'globalDatas' =&gt; null,
          ));
      }
   
      /**
       * {@inheritdoc}
       */
      public function getBlockPrefix()
      {
          return 'gac_adsbundle_vehiclead';
      }
   
   
  }</code></pre>
  </div>
  </div>
  </p>
  
  <blockquote>
    <blockquote>
      <blockquote>
        <p>The classified creation controller</p>
      </blockquote>
    </blockquote>
  </blockquote>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?php
   
  namespace Gac\AdsBundle\Controller;
   
  use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  use Symfony\Component\HttpFoundation\Request;
  use Symfony\Component\HttpFoundation\Response;
  use Gac\MediasBundle\Entity\Image;
  use Gac\MediasBundle\Form\ImageType;
  use Gac\AdsBundle\Entity\VehicleAd;
  use Gac\AdsBundle\Form\VehicleAdType;
  use Gac\VehiclesBundle\Entity\Vehicle;
  use Gac\VehiclesBundle\Form\VehicleType;
  use Gac\VehiclesBundle\Entity\VehicleModel;
  use Gac\VehiclesBundle\Entity\VehicleBrand;
   
  class ManageController extends Controller
  {  
      public function sellvehicleAction(Request $request)
      {
          $this-&gt;denyAccessUnlessGranted('ROLE_USER');
          $thisuser = $this-&gt;get('security.token_storage')-&gt;getToken()-&gt;getUser();
          $em = $this-&gt;getDoctrine()-&gt;getManager();
           
          // Create new ad and load a "mainImage"
          $classified = new VehicleAd($thisuser);
          $oneImage = new Image($thisuser);
          $classified-&gt;setMainImage($oneImage);
           
          $globalDatas = $this-&gt;container-&gt;get('gac_general.globaldatas');  // Always pass globalDatas VAR to any created form in the application
          $form = $this-&gt;createForm(VehicleAdType::class, $classified, array(
              'globalDatas' =&gt; $globalDatas,
          ));
           
          $form-&gt;handleRequest($request);
          if ($request-&gt;isMethod('POST') &amp;&amp; $form-&gt;isValid()) {
              $em-&gt;persist($classified);
              $em-&gt;flush();
               
              $id = $classified-&gt;getId();
               
              return $this-&gt;redirectToRoute('gac_ads_previewvehiclead', array('id' =&gt; $id));
          }
           
          return $this-&gt;render('ads/manage/sellvehicle.html.twig', array(
              'form' =&gt; $form-&gt;createView()
          ));
      }
  }</code></pre>
  </div>
  </div>
  </p>
  
  <blockquote>
    <blockquote>
      <blockquote>
        <p>And finally the view of my classified form</p>
      </blockquote>
    </blockquote>
  </blockquote>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>{% extends 'ads/layout.html.twig' %}
   
  {% block bundle_content %}
  &lt;form action="{{ path('gac_ads_sellvehicle') }}" method="post"&gt;
   
      &lt;div class="row justify-content-center"&gt;
          &lt;div class="col-md-8"&gt;
              {{ form_errors(form) }}
              &lt;div class="card mb-3"&gt;
                  &lt;div class="card-body"&gt;
                      &lt;p class="text-primary"&gt;&lt;strong&gt;{{ 'mandatory informations'|trans|capitalize }} :&lt;/strong&gt;&lt;/p&gt;
                      {{ form_row(form.vehicleBrand, {'label': 'Vehicle Brand :'}) }}
                      {{ form_row(form.vehicleModel, {'label': 'Vehicle Model :'}) }}
                  &lt;/div&gt;
              &lt;/div&gt;
               
              &lt;div class="card mb-3"&gt;
                  &lt;div class="card-body"&gt;
                      &lt;p class="text-primary"&gt;{{ 'image'|trans|capitalize }} :&lt;/p&gt;
                      &lt;div id="toto"&gt;
                          {{ form_row(form.mainImage.imageFile) }}
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
               
              &lt;div class="card mb-3"&gt;
                  &lt;div class="card-body"&gt;
                      &lt;p class="text-primary"&gt;{{ 'other parameters'|trans|capitalize }} ({{ 'optional'|trans }}) :&lt;/p&gt;
                      &lt;p class="text-primary"&gt;{{ 'tell us a few words about your vehicle'|trans|capitalize }} ({{ 'optional'|trans }}) :&lt;/p&gt;
                      {{ form_row(form.description, {'label': 'Description :'}) }}
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="col-md-4"&gt;
              &lt;div class="card mb-3"&gt;
                  &lt;div class="card-body"&gt;
                      {{ form_rest(form) }}
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;button type="submit" class="btn btn-block btn-primary"&gt;{{ 'preview your ad'|capitalize }}&lt;/button&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/form&gt;
  {% endblock bundle_content %}</code></pre>
  </div>
  </div>
  </p>
  
  <p>I must say that before asking, I did a lot of searching by myself (tests ...) and tons of google search. I know that there is some tricky stuff with Vich Uploader but I managed everything because ... it works for one file.</p>
  
  <p>So the question is : can you help me make my upload and my form work when embedded in another one ?</p>
  
  <p>Thanks a lot !</p>
  
  <p>Regards</p>