Search code examples
phpsymfonydoctrinevichuploaderbundle

symfony form with new files via Vich Uploader


Iam trying to build a smarter upload structure for my app. until now I had the code for uploading files in the controller and everything was more manual. Now I want to use the VichUploader in Symfony, but I have problems to implement it with multiple files.

First, I dont have an Entity with a file but an Entity that holds multiple File Entities. To be more clearly: The Entity Document has a oneToMany relation to File. So I build a form with a CollectionType:

$builder->add('files', Type\CollectionType::class, [
    'entry_type' => Type\FileType::class
])

But because there is not file yet when I add a new Document, no upload field is shown. And even when there are already files (on edit form), there shouldn't be upload fields shown but text fields with file names.

How can I achieve that? Do i still need to add an umapped field files_new() with a multiple FileType? Then the VichUploader automatic stuff would not work.


Solution

  • If you pass an empty file object you should have your html fil input as it is a collection

    In your controller (I don't have your code)

    $entity = new Entity()
    $entity->addFile(new FileEntity())//Add an empty file object
    
    
    $form = $this->createForm(YourType::class, $entity);
    $form->handleRequest($request);