'Upload multiple files form - Symfony 5
I have a multiple files upload working like a charm in EasyAdmin. But from the front-end side it doesn't want to upload files at all. I can see previously uploaded files and I see the "upload file" button, but after clicking save it gives me: "App\Entity\Uzytkownicy::addZalaczniki(): Argument #1 ($zalaczniki) must be of type App\Entity\Zalaczniki, Doctrine\ORM\PersistentCollection given, called in XXX\Controller\DefaultController.php on line 103"
in DefaultController.php
$uploadForm = $this->createForm(ZalacznikiKlientFormType::class, $user);
$uploadForm->handleRequest($req);
if ($uploadForm->isSubmitted() && $uploadForm->isValid()) {
***THIS IS LINE 103***->> $user->addZalaczniki($uploadForm->get('zalacznikis')->getData());
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($user);
$entityManager->flush();
try {$mailer->send($email);
} catch (TransportExceptionInterface $e) {
}
}
ZalacznikiKlientFormType.php
<?php
namespace App\Form;
use App\Entity\Uzytkownicy;
use App\Entity\Zalaczniki;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Vich\UploaderBundle\Form\Type\VichFileType;
class ZalacznikiKlientFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('zalacznikis', CollectionType::class, [
'entry_type' => ZalacznikiFormType::class,
'allow_add' => true,
'entry_options' => ['label' => false, 'allow_file_upload' => true],
'allow_delete' => true,
'required' => false,
'label'=> true,
'by_reference' => false,
'disabled' => false,
])
->add('Zapisz', SubmitType::class, [
'attr' => ['class' => 'btn btn-primary mt-1']])
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Uzytkownicy::class,
]);
}
}
ZalacznikiFormType.php
<?php
namespace App\Form;
use App\Entity\Zalaczniki;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Vich\UploaderBundle\Form\Type\VichFileType;
class ZalacznikiFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('File', VichFileType::class, array(
'label' => 'Plik',
))
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Zalaczniki::class,
]);
}
}
I can't find helpful information anywhere. I've found this How to upload multiple files with vich uploader in Symfony 5 but right now I'm hitting the wall.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
