1

I have a problem with editing an entity that has a file. I followed the Symfony documentation for uploading a file and it works fine. At creation my file is stored well in my upload folder and the encrypted name stored in database.

For the edition I followed the documentation so as not to have any error, I quote:

When creating a form to edit an already persisted item, the file form type still expects a File instance. As the persisted entity now contains only the relative file path, you first have to concatenate the configured upload path with the stored filename and create a new File class

And it worked.

But on the form's display, the file field says "No file selected"

Here is my controller :

public function editAction(Request $request, Produits $entity)
    {
        $entity->setPhoto(
            new File($this->getParameter('images_directory').'/'.$entity->getPhoto())
        );
        $form = $this->createForm('ProjectBundle\Form\Produit\ProduitType', $entity);

        $form->handleRequest($request);
        if ($form->isSubmitted()) {
            if ($form->isValid()) {
                $em = $this->getDoctrine()->getManager();
                $em->persist($entity);
                $em->flush();

                $this->addFlash('success', 'Le produit "'.$entity.'" a été modifié.');

                return $this->redirectToRoute('produit_show', array('id' => $entity->getId()));
            }
        } elseif ($form->isSubmitted()) {
            $this->addFlash('error', 'Le produit "'.$entity.'" n\'a pas pu être modifié.');
        }

        return $this->render('ProjectBundle:admin/Produit:edit.html.twig', array(
            'entity' => $entity,
            'form' => $form->createView(),
        ));
    }

And in my view :

<div class="input-field{% if form.photo.vars.errors|length %} has-error{% endif %}">
    {{ form_widget(form.photo,{'attr': {'class': 'form-control'}}) }}
    {{ form_errors(form.photo) }}
</div>

If I do a {{ dump(form.photo) }} I get all information about the photo belonging to my entity.

Do you have an idea to get the name of the image in the field ?

Thanks !

Peter Artoung
  • 224
  • 5
  • 20
  • Hi, did you find any solution? I am stacked at the same point :-( – mario Feb 09 '17 at 15:25
  • Actually, I think you can not "get the name of the image in the field", by [these reasons](http://stackoverflow.com/questions/1696877/how-to-set-a-value-to-a-file-input-in-html). What you may wish is to show the picture, say, just below the "No file selected" string. In particular, if you have a form to edit multiple objects at once, it would be nice to display the different pictures. Unfortunately, I am not sure how to do that. – mario Feb 09 '17 at 15:53
  • Indeed, the puzzling issue is why it seems not to be possible to display something like {{ form.photo.name }}, although it is clear how to display {{ dump(form.photo) }} and the latter shows photo.name too. – mario Feb 09 '17 at 16:05
  • could you fix it? – Juan Fernandez Sosa Jan 24 '18 at 16:37

3 Answers3

0

This is the most logic behavior...

Files that are displayed in the fileType input are local files (from your computer) before you send them to the server.

If you want to "display" the current file, add a custom field which will display the filename when one exists.

Preciel
  • 2,666
  • 3
  • 20
  • 45
0

This line doesn't appear to be correct

    $entity->setPhoto(
        new File($this->getParameter('images_directory').'/'.$entity->getPhoto())
    );

if you are calling $entity->getPhoto() you are implying that a file already exists, however you are setting a new file to the entity. My thought is that the file is not being found and therefore no name is displaying.

If you added the FormType and Model it may help to determine the problem.

Ryguydg
  • 103
  • 1
  • 8
  • No, this is exactly what you are advised to do by the [docu](http://symfony.com/doc/current/controller/upload_file.html). You are setting a new file with info about the file previously uploaded. This is fine, since you want to render a **edit** form. However, to me, it is not clear how you insert that piece on info within the form. – mario Feb 09 '17 at 15:37
0

I have make a workaorund to solve this topic, I couldn't manage to pass the File in the edit form, so i show the current image and in case the user don't upload a new one saves the old one.

I have an Army entity, in my twig i call the image with

<img style="max-width:128px" src={{ asset('uploads/army/' ~ army.image.filename) }}>

and my edit function in the controller.

   public function editAction(Request $request, Army $army)
{
    $army->setImage(new File($this->getParameter('army_directory') . '/' . $army->getImage()));
    $imgOri = basename($army->getImage());
    $deleteForm = $this->createDeleteForm($army);
    $editForm = $this->createForm('ArmyDataBundle\Form\ArmyType', $army);
    $editForm->handleRequest($request);

    if ($editForm->isSubmitted() && $editForm->isValid()) {

        if ($army->getImage()) {
            // $file stores the uploaded PDF file
            /** @var Symfony\Component\HttpFoundation\File\UploadedFile $file */
            $file = $army->getImage();

            // Generate a unique name for the file before saving it
            $fileName = md5(uniqid()) . '.' . $file->guessExtension();

            // Move the file to the directory where army are stored
            $imgRoute = $this->container->getParameter('army_directory');
            $file->move(
                $imgRoute,
                $fileName
            );
            $army->setImage($fileName);
        } else {

            $army->setImage($imgOri);
        }
        $this->getDoctrine()->getManager()->persist($army);
        $this->getDoctrine()->getManager()->flush($army);

        return $this->redirectToRoute('army_show', array('id' => $army->getId()));
    }

    return $this->render('@ArmyData/Army/edit_army.html.twig', array(
        'army' => $army,
        'edit_form' => $editForm->createView(),
        'delete_form' => $deleteForm->createView(),
    ));
}