Multiple file upload with Symfony2

后端 未结 11 820
温柔的废话
温柔的废话 2020-12-01 02:12

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 Symf

相关标签:
11条回答
  • 2020-12-01 02:44

    All the suggestions I've found here are workarounds for the real situation.

    In order to be able to have multiple attachments, you should use form collection.

    Quote from the documentation:

    In this entry, you'll learn how to create a form that embeds a collection of many other forms. This could be useful, for example, if you had a Task class and you wanted to edit/create/remove many Tag objects related to that Task, right inside the same form.

    http://symfony.com/doc/2.0/cookbook/form/form_collections.html

    Example case: You have a document, which form is specified by DocumentType. The document must have multiple attachments, which you can have by defining AttachmentType form and adding it as a collection to the DocumentType form.

    0 讨论(0)
  • 2020-12-01 02:44

    Symfony introduced 'multiple' option to file field type in symfony 2.5

    $builder->add('file', 'file', array('multiple' => TRUE));
    
    0 讨论(0)
  • 2020-12-01 02:46

    use this methode :

    $form = $this->createFormBuilder()
    ->add('attachments','file', array('required' => true,"attr" => array(
      "multiple" => "multiple",
      )))
    ->add('save', 'submit', array(
      'attr' => array('class' => 'btn btn-primary btn-block btn-lg'),
      'label' => 'save'
      ))
    ->getForm();
    

    then you add [] to the name of your input via jQuery :

    <input id="form_attachments" name="form[attachments]" required="required" multiple="multiple" type="file">
    

    jQuery code :

     <script>
      $(document).ready(function() {
        $('#form_attachments').attr('name',$('#form_attachments').attr('name')+"[]");
      });
    </script>
    
    0 讨论(0)
  • 2020-12-01 02:48

    For sf > 2.2 : In you form type class, add this overrided method :

    public function finishView(FormView $view, FormInterface $form, array $options) {
        $view->vars['form']->children['files']->vars['full_name'] .= '[]';
    }
    
    0 讨论(0)
  • 2020-12-01 02:49

    No extra classes needed (except the gallery_manger service but the issue you describe happens before...)

    I don't really know what's wrong. Check for your template (maybe wrong enctype... or name attr missmatching)

    first try to do a single file upload, check the documentation:

    1. file Field Type
    2. How to handle File Uploads with Doctrine

    Once it works, you have to edit some lines.

    1. add input file multiple attribute.
    2. append [] at the end of the input file name attribute (mine is create...[] because my form class name is create, if your is createType it will be createType...[])
    3. init $files as an array.

    Copy/paste your code here.

    0 讨论(0)
  • 2020-12-01 02:52

    What would happen if there would be some validation errors? Will Symfony Form repost multiple file upload field. Because I tried it and I think for this purpose you need to use collection of file fields. Than symfony form must render all fields have added before correctly.

    0 讨论(0)
提交回复
热议问题