VichUploaderBundle
VichUploaderBundle copied to clipboard
Form to multi-image upload
I'm using VichUploaderBundle in Symfony 4 and I have a question about using a form to send in a bunch of photos. To do this I have to use CollectionType and a one-to-many relationship (One product has several photos). Below I present the code which only shows me an empty frame of the form, and it lacks buttons for adding photos as they are in this uploader. I do not understand why this is happening.
Product entity (one-to-many):
/**
* @var ProductMultiImage[]
* One Product has Many Images.
* @ORM\OneToMany(targetEntity="App\Entity\Image\ProductMultiImage", mappedBy="product", cascade={"persist"})
* @ORM\JoinColumn(nullable=true)
*/
private $productMultiImages = null;
Image entity (many-to-one):
/**
* Many Images have One Product.
* @ORM\ManyToOne(targetEntity="App\Entity\Admin\Product", inversedBy="productMultiImages", cascade={"persist"})
* @ORM\JoinColumn(name="product_id", referencedColumnName="id", nullable=true)
*/
private $product = null;
Form for adding photos:
class ProductImageType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('productMultiImages', CollectionType::class, array(
'data_class' => ImageType::class,
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
))
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => null,
]);
}
}
ImageType:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('imageFile', VichImageType::class)
;
}
Where i can find any solution ?
Did you try to manually POST your form? I'm afraid that this bundle can't solve any frontend related issue, like your form rendering. Of course, double checking with Symfony docs could help.
I did not try it, I was looking for a solution based on Symfony Forms. So I do not have to manually (without symfony form ) draw the form in Twig {% for ()%} and write them manually?
You need to follow Symfony docs for forms. But this is, unfortunately, unrelated to this bundle. Symfony itself offers many channels of support.