I'm trying to upload multiple files via a form, but I can only upload one file at a time, the last one I mark in the browser. Is there a way to upload more images with Symfony2 using a simple form? Here is the twig template of the form I'm using to be able to mark more than one file:
{{ form_widget(form.post_image, { 'attr': {'multiple': 'multiple' }}) }}
Methods
getChild
andset()
were removed in 2.3. Instead of this you should usechildren[]
andvars
propertiesbefore:
after:
Here is easy example to upload multiple files. I have similar problem with upload files.
https://github.com/marekz/example_symfony_multiply_files_example
For symfony 3.*
First: Both form declatartion:
Now, my entities:
Finaly, Symfony Controller:
use this methode :
then you add [] to the name of your input via jQuery :
jQuery code :
Note that i try to do the same thing in sf2 using this syntax:
In the controller:
$files
will be an array of uploaded files...Calling
$form->createView()
to alter the name attribute is certainly not the best way / cleanest way to do it but it's the only one i found that keeps the csrf functionality working, because altering the name attribute in a twig template makes it invalid...Now I still have an issue using a form class which generate the form, I don't know why during the binding of the form data & object attached to the form my array of uploaded files is transformed in array of (file) name ???
For sf > 2.2 : In you form type class, add this overrided method :