Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony - The file could not be found during update

Tags:

php

symfony

I created an entity Document with a list of attributes including file attribute, while adding a document goes very well, when i update i get the validation error :

The file could not be found.

file attribute must be required while adding but optional in edit, because i can just keep the old file.

Here is a part of my entity Document :

/**
 * @ORM\Entity
 * @ORM\Table(name="document")
 */
class Document
{
    ...

    /**
     * @var string
     * @Assert\NotBlank()
     * @Assert\File(maxSize = "5M", mimeTypes = {"application/pdf"})
     * @ORM\Column(name="file", type="string", length=255, nullable=true)
     */
    private $file;

    /**
     * @var string
     * @ORM\Column(name="name", type="string", length=50)
     */
    private $name;

    /**
     * @ORM\ManyToOne(targetEntity="Dtype", inversedBy="documents")
     */
    private $dtype;

...

public function uploadFile($path, $type='', $oldFile=null)
{
    $file = $this->getFile();
    if ($file instanceof UploadedFile) {
        if(!empty($type)){
            $path = $path. '/' . $type;
        }
        $fileName = md5(uniqid()).'.'.$file->guessExtension();
        $file->move($path, $fileName);
        $this->setFile($type. '/' .$fileName);
        if($oldFile !== null){
            $oldFilePath = $path .'/'. $oldFile;
            if(file_exists($oldFilePath))
                unlink($oldFilePath);
        }
    }else{
        $this->setFile($oldFile);
    }

}

and in Controller i have :

public function editAction(Request $request, Document $document) {
    $oldFile = $document->getFile();
    $form = $this->createForm('AppBundle\Form\DocumentType', $document);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $document->uploadFile($this->getParameter('documents_file_dir'), $document->getDtype()->getChemin(), $oldFile);
        $em = $this->getDoctrine()->getManager();
        $em->persist($document);
        $em->flush();
    }
...
}

Any help please ?


EDIT

The behavior iam wondering while a document is updated :

If the user has updated the file, then the file attribute must be validated with @Assert\File,

Else the file attribute will not be validated, so i can keep the original file uploaded while creating the document.

like image 484
Boubouh Karim Avatar asked Nov 16 '16 11:11

Boubouh Karim


2 Answers

Use the validation groups. Mark field as required file only for create a new document:

/**
 * ...
 * @Assert\File(maxSize = "5M", mimeTypes = {"application/pdf"}, groups = {"create"})
 * ...
 */
private $file;

And specify relevant group for create and edit actions:

public function editAction(Request $request) {
    // ...
    $form = $this->createForm('AppBundle\Form\DocumentType', $document, ["create"]);

// ...

public function editAction(Request $request, Document $document) {
    // ...
    $form = $this->createForm('AppBundle\Form\DocumentType', $document, ["edit"]);
like image 135
Timurib Avatar answered Oct 16 '22 23:10

Timurib


I had the same problem but in the documentation, there is the answer : https://symfony.com/doc/current/controller/upload_file.html

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:

use Symfony\Component\HttpFoundation\File\File;
// ...

$product->setBrochure(
    new File(_YOUR_UPLOAD_DIRECTORY_.'/'.$product->getBrochure())
);

Add it just before your createForm

$document->setFile(
    new File(_YOUR_UPLOAD_DIRECTORY_.'/'.$document->getFile())
);
$form = $this->createForm('AppBundle\Form\DocumentType', $document);

Hope to resolve your problem.

like image 4
Skyd Avatar answered Oct 17 '22 00:10

Skyd