5
votes

I'm trying to make a form with a multiple file field. Since the docs are quite vague:

I've decided to use W3C FileReader API (Based in the doc urls below), to handle files from the client and manage the underliying data from the view to the entity. Currently supports drag&drop, metadata, and multiple selections on the client.

But I want to give the UploadedFile object one more chance, and the MAIN problem I have is that I can't make the file property in my entity (FileUpload type) to store more than one file data. My input looks like this:

<input type="file" id="upload_files" name="upload[files][]" required="required" multiple="multiple" />

In theory If I made the name to be an Array, the fileUpload should contain the files but it doesn't. Can UploadedFile object store multiple files data ? or just single ?

http://api.symfony.com/2.2/Symfony/Component/HttpFoundation/File/UploadedFile.html

Also tried to initialize (in the __construct of the entity) the $files property as an array and modify the setFiles() to store new array index's $this->files[] = $file; ... you know.

But then Symfony tells me that exception:

The form's view data is expected to be an instance of class Symfony\Component\HttpFoundation\File\File, but is a(n) array. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms a(n) array to an instance of Symfony\Component\HttpFoundation\File\File.

I'm not familiarized with data transformers. And Can't figure it out how can be done now. Or If it could be really useful to get the UploadedFile Object with every file data.

In synthesis... With this given info, and the code below. Could anyone help me to get the FileUpload object the correct number of files and not just the last added ? Thank You

Made a repo on github: https://github.com/jeflopo/fileupload

For brevity, here are the relevant files:

1
Thanks, It's a practical and good example, But It uses a collection type. That means a bunch of single field files. I need just one field with the multiple attribute.Jeflopo

1 Answers

3
votes

I created a pull request which fixes the error!

What's left to do now is that you create unique filenames for the uploaded files and then use the move method on each file. If you don't move them the files won't get saved!

As I set mapped to false, your entity doesn't contain the files. You have to create an array with the filenames you just created to save the file paths.