V-labs / VlabsMediaBundle

Form, database and filesystem abstraction for files
44 stars 18 forks source link

upload is not remembered, image is on form error #34

Closed rbaarsma closed 10 years ago

rbaarsma commented 10 years ago

Hey,

When I upload an image in a standard symfony form with this bundle, and I have a validation error on any field, after reload it shows the image, but you need to re-upload the image.

Behavior should be that if the image was correctly uploaded it should persist, even if other fields throw validation errors and upon solving those validation errors the form should save normally, WITH image.

rbaarsma commented 10 years ago

Well I've been on this a few hours. I first worked on a solution in the controller. This I eventually created:

    $form->setRequest($request);

    // check all fields of the form 
    $fname = $form->getName();
    foreach($form->getIterator() as $col=>$val)
    {
        $getter = 'get'.ucfirst(str_replace("_","",$col));
        $val = $entity->$getter();
        $sess_name = "_spin_file_upload_{$fname}_$col";

        // if session was already created
        if ($this->get('session')->has($sess_name))
        {
            if ($val === null)
            {
                $setter = 'set'.ucfirst(str_replace("_","",$col));
                $entity->$setter( ( $this->get('session')->get($sess_name) ) );
            }

            // remove the session if the form is valid
            // note: when the user closes the browser on validation errors the session remains.
            if ($form->isValid())
                $this->get('session')->set($sess_name, null);
        }

        // save all images to session, so we can retrieve them if form has validation errors
        if ($val instanceof Picture && !$form->isValid())
            $this->get('session')->set($sess_name, $val);
    }

    // continue normal form action
    if ($form->isValid())
    {

I've also tried to get this same function in the EventSubscriber structure of the forms, but somehow I can't get it to work there. I hope this helps

choomz commented 10 years ago

Hi,

It looks strange, image in a form is a standard behavior of this bundle. It looks like a configuration error.

Can you please paste me your config for this field ?