Problems With Multiple File Upload In Symfony2

久未见 提交于 2019-11-28 06:58:07

This is a known issue as referenced on GitHub.

As they say, you should append [] to the full_name attribute in your template :

{{ form_widget(images_form.file, { 'full_name': images_form.file.get('full_name') ~ '[]' }) }}

I do not know if that is possible with the annotation syntax. So I am going to write it in in plain PHP in the controller

$images_form = $this->createFormBuilder($file)
    ->add('file', 'file', array(
        'constraints' => array(
            new NotBlank(), // Makes sure it is filled at all.
            new All(array( // Validates each an every entry in the array that is uploaded with the given constraints.
                'constraints' => array(
                    new File(array(
                        'maxSize' => 6000000
                    )),
                ),
            )),
        ),
        'multiple' => TRUE,
    ))
    ->getForm();

This should work perfectly since Symfony 2.4. Before that you would have to put the multiple attribute in the attr key like you already did.

As I said, you have to to make this work with annotations. It might work but could be less readable if you have to put it all in one line.

Have fun ;)

goldquest

I had the same problem recently, followed the suggestions here, but got an error, because the validator 'file' can't handle arrays.

So I had to write my own validator, which can handle multiple files. For that, I followed this tutorial from Symfony.com, copy/pasted the code from validator 'file' in it, encapsulated it with a foreach loop and changed the variables as needed.

If you do this, you can use it for $file in your entity.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!