Giter Site home page Giter Site logo

Comments (11)

craue avatar craue commented on May 20, 2024

All step data is serialized into the session. But unfortunately, just serializing file uploads is not possible. You'll have to implement file upload handling yourself and immediately move the uploaded file somewhere. I haven't done that myself using a flow yet.

from craueformflowbundle.

craue avatar craue commented on May 20, 2024

@madarco: Did you find a way to achieve what you wanted?

from craueformflowbundle.

madarco avatar madarco commented on May 20, 2024

Only with a workaround: I check if there is a file to upload and set the value in the session.
Then, before $flow->bind($values); I do:

 $values->setUploadedPhoto($this->getRequest()->getSession()->get('tmpUploadedFile'));

And when I do $flow->reset(), I also do:

 $this->getRequest()->getSession()->remove('tmpUploadedFile');

from craueformflowbundle.

craue avatar craue commented on May 20, 2024

So, if there's an uploaded file, you move it and save its path in the session? Do you also handle the case that a file is uploaded but the flow isn't finished (e.g. if the user just leaves the page)?

from craueformflowbundle.

madarco avatar madarco commented on May 20, 2024

No, in that case the file and the session variable will remain there.

I use MongoDB GridFS to store files, so I'll run a job that check for spurious files and remove them...

Not the best solution :)

from craueformflowbundle.

craue avatar craue commented on May 20, 2024

Alright, but it seems to get the job done. ;)

from craueformflowbundle.

tristanbes avatar tristanbes commented on May 20, 2024

@craue @madarco So, what's the status on this ? I'm facing the same problem with NULL values on the file field.
Can I use your temp solution madarco ?

from craueformflowbundle.

madarco avatar madarco commented on May 20, 2024

yes @tristanbes
It is actually working for me on production

from craueformflowbundle.

oopsFrogs avatar oopsFrogs commented on May 20, 2024

Hi, @madarco, I'm a beginner for symfony2, and I also faced the same problem with you. However, I can't figure out how to walkaround with your method. Would you please do me a favor to post detailed code? It would be highly appreciated.

from craueformflowbundle.

madarco avatar madarco commented on May 20, 2024

Sure @oopsFrogs, sorry for the late reply.

My fix is that:

  • on every step of the flow check if the file field is in the request
  • if it is in the request method (ie: was submitted in the html form), save the file in a temp directory, and save in the session the actual path to the file.
  • in the last step, get the value from the session and save the value in your Document or Entity.

This is the code:

What I do is to call a method after the check for $flow->isValid($form) in your action, so that it is executed on every step of the flow:

    if ($flow->isValid($form)) {

        //XXX: fix for unknown bug in FormFlow, we'll handle the photo field manually (not in the Flow class):
        $photo = $this->_fixUploadFile($values->getUploadedPhoto());
        if($photo) {
            $values->setUploadedPhoto($photo);
        }
        //ENDIFX

        if ($flow->nextStep()) {
            ...
        //omissis: check if nextStep()...
        }

        //All steps completed:
        $dm->persist($values);

        //Important!!! clear the variable from the session when you have done:
        $this->getRequest()->getSession()->remove('tmpUploadedFile');
        $dm->flush();

        return $this->redirect(completeUrl);
     }

     //XXX: exec the fix also here, so that in case of validation errors, the user can see the file uploaded in the form:
     $photo = $this->_fixUploadFile($values->getUploadedPhoto());
     if($photo) {
          $values->setUploadedPhoto($photo);
      }
      //ENDIFX

      return array('form' => $form->createView(), 'flow' => $flow, 'values' => $values);

The method _fixUploadFile simply save the file and store in the session the actual file path, to be used in the last step of the flow (when you save it in the db).

This is my method, I upload the file to mongoDb, but the same applies if you save the file on disk too:

    public function _fixUploadFile($file) {

        if(!empty($file) && $file instanceof UploadedFile) {

            $picture = new Picture();
            $picture->setFile($file->getPathname());
            $picture->setExtension($file->guessExtension());
            $picture->setMimetype($file->getMimeType());
            $picture->setType('profile-upload');
            $picture->setSizeType('original');


            $imagine = new Imagine();
            $size = $imagine->open($file->getPathname())->getSize();
            $picture->setWidth($size->getWidth());
            $picture->setHeight($size->getHeight());
            $picture->setOriginalName($file->getClientOriginalName());

            $this->get('doctrine.odm.mongodb.document_manager')->persist($picture);
            $this->get('doctrine.odm.mongodb.document_manager')->flush();       

            $this->getRequest()->getSession()->set('tmpUploadedFile', "" . $picture->getId());
        }
        return $this->getRequest()->getSession()->get('tmpUploadedFile');

    }

from craueformflowbundle.

oopsFrogs avatar oopsFrogs commented on May 20, 2024

Thank you, @madarco

from craueformflowbundle.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.