Symfony2 Forms: Show already downloaded files if the form is invalid.

I am creating a complex form for uploading files. This form exists from ordinary data and a set of 4 file types with the ability to add multiple files for each type.

  • REPORTFORM
    • Attachments
      • Reports (collection of UploadedFile objects)
      • Photos (collection of UploadedFile objects)

The UploadedFile object has a file name, description, etc.

Use case: Now I submit a form with 4 photos and 2 reports, all 6 files are in order. But there are other errors in the form. But I want to show downloaded files again so that the user cannot reload them again. These files are already saved, so when I return to the same page with GET, the files are displayed correctly.

What I have already done / tried:

  • Before binding the request, I clone the existing downloaded files.
  • After binding the form, I upload all the new files (if any) and save them
  • After that, I re-add the existing uploaded files (from the cloned object). I created the form input with an existing file identifier, so I can recognize existing elements.

This all works fine, BUT, as I change my report and attachments AFTER linking the data displayed after the message cannot be changed. I can not do setData ($ report) in the form presented.

So, existing files are no longer displayed, since form data is still the old report object (from the message).

When I do a normal GET page, the $ report is retrieved from the database and displayed correctly. But after the POST with the already downloaded files, the database data is correct, but the form view does not know anything about the changed data (after binding).

Any ideas? Or is the best way to do this?

+6
source share
2 answers

Well, after several days of trail and error, I found a suitable solution.

Here's what I did, in short.

In essence UploadedFile I added 2 new fields: file and uniqueId . These fields are not displayed in ORM, since I only need them in the form.

When submitting the form, I add the recently uploaded file to a specific var session and assign them a unique identifier (because I need to delete them and I need the identifier). I am also uploading files to a new location.

If the form is correct, I add this collection, which I saved in the session, to the entity that contains this collection.

If the form is incorrect, the files are displayed because I am retrieving this particular collection from the session. Even when I revise the page using GET, all downloaded files are displayed.

If I delete the file in the view, I will put the list of deleted unique identifiers in the input field. Therefore, after sending, I check which files I should delete from the array.

I think my answer is as obscure as my question :)

+3
source

I made the decision below. You can adapt it to your needs. But you need an additional command that clears old files in the uploads / tmp directory. And you need two parameters in the parameters section in config:

parameters: uploads_dir: %kernel.root_dir%/../web/uploads uploads_tmp_dir: %uploads_dir%/tmp 

The form:

 public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('file',ImageFileType::TYPE_NAME,array( 'fileField' => 'file', 'pathField' => 'path', 'required' => false, 'label' => 'makeswapping.form.single_image', 'imgclass' => 'tumbnail imgbox full-width', 'constraints' => array( new Assert\Image(array( 'minWidth' => '440' )) ) )); $container = $this->container; $builder->addEventListener( FormEvents::PRE_SUBMIT, function (FormEvent $event) use ($container) { $form = $event->getForm(); $data = $event->getData(); $uploadsTmpDir = $container->getParameter('uploads_tmp_dir'); if (isset($data['file'])) { $filename = sprintf('%s.%s', uniqid('tmp_'), $data['file']->getClientOriginalExtension()); if (!file_exists($uploadsTmpDir)) { mkdir($uploadsTmpDir, 0755, true); } copy($data['file']->getRealPath(), $uploadsTmpDir . '/' . $filename); $form->add('file_hidden', 'hidden', array( 'mapped' => false, 'required' => false, 'empty_data' => $filename, )); } else if (!isset($data['file']) && isset($data['file_hidden'])) { $fileInfo = new \SplFileInfo($uploadsTmpDir . '/' . $data['file_hidden']); $mimeTypeGuesser = MimeTypeGuesser::getInstance(); $uploadedFile = new UploadedFile( $fileInfo->getRealPath(), $fileInfo->getBasename(), $mimeTypeGuesser->guess($fileInfo->getRealPath()), $fileInfo->getSize(), null, true ); $form->add('file_hidden', 'hidden', array( 'mapped' => false, 'required' => false, 'empty_data' => $data['file_hidden'], )); $form->get('file')->setData($uploadedFile); $data['file'] = $uploadedFile; $event->setData($data); } } ); } 
+3
source

Source: https://habr.com/ru/post/956152/


All Articles