简体   繁体   中英

Symfony3 Sending a Form from the Controller

EDIT :

I found the $form->submit() method and gave it a try :

file_put_contents(__DIR__ . "/../../../web/uploads/img/tmp.jpeg", file_get_contents($output['images']['show']));

$image = new Image();;

$form = $this->createForm(ImageType::class, $image);

$form->submit( __DIR__ . "/../../../web/uploads/img/tmp.jpeg" );

if ($form->isSubmited() && $form->isValid()) {
    ...
}

But apparently the form isn't valid because it doesn't get past the condition

EDIT 2 : I disabled csrf_protection for ImageType but it's still not valid, Xdebug tells me the path is not valid, but if I try to upload the same image from the same path through a form it works without problem...


I'm working on a project where I have this Image entity :

class Image
{
    //... Attributes, setters and getters        

    /**
     * @ORM\PrePersist()
     * @ORM\PreUpdate()
     */
    public function preUpload()
    {
        if (null === $this->file) {
            return;
        }
        $this->url = $this->file->guessExtension();
        $this->alt = $this->file->getClientOriginalName();
    }

    /**
     * @ORM\PostPersist()
     * @ORM\PostUpdate()
     */
    public function upload()
    {
        if (null === $this->file) {
            return;
        }
        if (null !== $this->tempFilename) {
            $oldFile = $this->getUploadRootDir().'/'.$this->id.'.'.$this->tempFilename;
            if (file_exists($oldFile)) {
                unlink($oldFile);
            }
        }
        $this->file->move(
            $this->getUploadRootDir(),
            $this->id.'.'.$this->url
        );
    }

    /**
     * @param UploadedFile $file
     */
    public function setFile(UploadedFile $file)
    {
        $this->file = $file;
        if (null !== $this->url) {
            $this->tempFilename = $this->url;
            $this->url = null;
            $this->alt = null;
        }
    }
}

Which works perfectly fine when I'm uploading the image from a local file through a Form.

But I also need to be able to download the file from an API :

file_put_contents(__DIR__ . "/../../../web/uploads/img/tmp.jpeg", file_get_contents($output['images']['show']));

$image = new Image();

$file = new UploadedFile(__DIR__ . "/../../../web/uploads/img/tmp.jpeg", 'tmp.jpeg');

$image->setFile($file);

It is my understanding that once I persist $image, both preUpload() and upload() will be called, but when I do I get this error :

"The file "tmp.jpeg" was not uploaded due to an unknown error."

Which seems to be triggered by the $this->file->move() part of upload()

I'm pretty sure it is because of the isValid() function from UploadedFile which only returns true if the file has been uploaded with HTTP and is called right at the start of move()...

Is there any workaround for my issue ? I'm fairly new to Symfony (and a bit less at coding in general) so I'm not entirely sure of what my options are.

Is there a way to trick $image into "thinking" $file came from a Form ?

自从我专门执行此操作已经有一段时间了,但是我认为,如果要使其有效,则在手动创建UploadedFile时必须在构造函数中具有模仿类型。

$file = new UploadedFile(__DIR__ . "/../../../web/uploads/img/tmp.jpeg", 'tmp.jpeg','image/jpeg');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM