Skip to content

Instantly share code, notes, and snippets.

@jverdeyen
Last active December 25, 2015 04:39
Show Gist options
  • Save jverdeyen/6918475 to your computer and use it in GitHub Desktop.
Save jverdeyen/6918475 to your computer and use it in GitHub Desktop.
A problem with Symfony2 form collections and multiple file uploads

I'm having a problem with multiple file uploads in a collection form type.

This is the case:

  • Report
    • Attachments
      • Files (UploadedFile)
      • Photos (UploadedFile)

A report object has an Attachment object, the Attachment object has multiple in ArrayCollections (ManyToMany) with UploadedFile object in it.

So..

I made some custom form types: ReportType AttachmentType UploadedFileType (because file can have description)

ReportType:

$builder->add('attachments', 'attachments_type');
...

AttachmentType:

$builder->add('photos', 'collection', array(
            'label' => 'report.attachments.photos',
            'type' => 'uploadedfile_type',
            'allow_add' => true,
            'allow_delete' => true,
            'prototype' => true,
            'options' => array(
                'required' => false,
                'description' => true
            )
        ));

        $builder->add('files', 'collection', array(
            'label' => 'report.attachments.files',
            'type' => 'uploadedfile_type',
            'allow_add' => true,
            'allow_delete' => true,
            'prototype' => true,
            'options' => array(
                'required' => false,
                'description' => false
            )
        ));
...

UploadedFileType:

$builder->add('filename', 'file', array(
            'required' => false,
            'data_class' => null,
            ));

        $builder->add('originalFilename', 'hidden', array());

        $builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event){
            $form = $event->getForm();
            $options = $form->getConfig()->getOptions();
            $description = $options['description'];

            if ($description === true) {
                $form->add('description', 'textarea', array(
                    'required' => false,
                ));
            }
        });

What I want to achieve:

A visitor can add multiple files for each type of attachment. When a user submits the form and validation errors appear, files that are validated and are ok should be uploaded and show as 'originalfilename.txt --remove---'. So when u user forgot to fill in his email and submits 20 files... he shouldn't add all the files all over again. This means file uploads are done before the form validation.

But: I can't find a solid solution to make this work. Creating a datatransformer doesn't work, because the datatransformer should only be triggered when a file is already uploaded (and saved in the database).

Because I save these file paths in the database even before validation, it should also be possible to refresh the page (without POST) and see all the already uploaded files.

Handling the add/remove in javascript is working, adding file input fields is like you should do with a normal collection form type. But handling the already uploaded files as normal strings (filename) and not overwriting them after a re-submit doesn't seem to work properly.

Any advice is welcome :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment