Yash Sharma Yash Sharma - 1 month ago 6x
PHP Question

Image upload must required validation

I'm trying to make image upload compulsory. I've tried it on a Collection Type by

which was generating an error
Image is missing!
on the top of the form i.e. error bubbling then I removed
error bubbling
by setting it
. After removing error bubbling I'm unable to populate image missing error. How can I map the error to its form element i.e.

Please help & provide me possible solutions for this problem by using Symfony or jQuery. And also suggest the which one is best & why.

Entity: XYZBundle\Entity\Abc.php

Class Abc

* @JMS\Groups({"details"})
* @JMS\Expose
* @var \Doctrine\Common\Collections\Collection
private $images;


FormType: XYZBundle\Entity\AbcType.php

public function buildForm(FormBuilderInterface $builder, array $options)

->add('images', CollectionType::class, array(
'entry_type' => MediaType::class,
'label' => false,
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'prototype' => true,
'error_bubbling' => false,




By adding the following code into AbcController I'm able to show the error now. And I've now removed the @Assert\Valid from the Entity (XYZBundle\Entity\Abc.php).

Controller: AbcController.php

 $images = $form['images']->getData();
            foreach ($images as $image){
                $form['images']->addError(new FormError("Please upload at least one image."));

The only thing that is remaining is to map the error just after its form element.