首页 > 解决方案 > 提交表单而不选择任何文件时,VichUploaderBundle 错误“类型为“文件”的预期参数,给出“NULL””

问题描述

我正在集成VichUploaderBundle,并且我已经成功地能够提交文件并保存、下载并编辑表单,重新提交另一个文件并替换原始文件。

但是当我提交表单而不附加任何文件时,我收到以下错误:

Expected argument of type "Symfony\Component\HttpFoundation\File\File", "NULL" given at property path "attachmentFile".

 $form->handleRequest($request);

这就是我在表单中添加组件的方式:

$builder->add('attachmentFile', VichFileType::class, [
    'label' => 'Upload file',
    'allow_delete' => false,
    'required' => false
]);

这是我的实体:


/**
 * @ORM\Entity(repositoryClass="App\Repository\Campus\Material\DocumentRepository")
 * @ORM\Table(name="campus_publication_document")
 * @Vich\Uploadable()
 */
class Document extends Publication
{

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

    /**
     * @var int
     * @ORM\Column(type="integer")
     */
    private $attachmentSize;

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

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

    /**
     * @Vich\UploadableField(
     *     mapping="tw_campus_document",
     *     fileNameProperty="attachmentName",
     *     size="attachmentSize",
     *     mimeType="attachmentMime",
     *     originalName="attachmentOriginalName"
     * )
     * @var File
     */
    private $attachmentFile;

    /**
     * @ORM\Column(type="datetime")
     * @var \DateTime
     */
    private $attachmentUploadedAt;

    /**
     * @return string
     */
    public function getAttachmentName(): ?string
    {
        return $this->attachmentName;
    }

    /**
     * @param string $attachmentName
     */
    public function setAttachmentName(string $attachmentName = null): void
    {
        $this->attachmentName = $attachmentName;
    }

    /**
     * @return int
     */
    public function getAttachmentSize(): int
    {
        return $this->attachmentSize;
    }

    /**
     * @param int $attachmentSize
     */
    public function setAttachmentSize(int $attachmentSize = null): void
    {
        $this->attachmentSize = $attachmentSize ?: 0;
    }

    /**
     * @return string
     */
    public function getAttachmentMime(): string
    {
        return $this->attachmentMime;
    }

    /**
     * @param string $attachmentMime
     */
    public function setAttachmentMime(string $attachmentMime = null): void
    {
        $this->attachmentMime = $attachmentMime;
    }

    /**
     * @return string
     */
    public function getAttachmentOriginalName(): string
    {
        return $this->attachmentOriginalName;
    }

    /**
     * @param string $attachmentOriginalName
     */
    public function setAttachmentOriginalName(string $attachmentOriginalName = null): void
    {
        $this->attachmentOriginalName = $attachmentOriginalName;
    }

    /**
     * @return File
     */
    public function getAttachmentFile(): ?File
    {
        return $this->attachmentFile;
    }

    /**
     * @param File $attachmentFile
     * @throws \Exception
     */
    public function setAttachmentFile(File $attachmentFile): void
    {
        $this->attachmentFile = $attachmentFile;
        if ($attachmentFile){
            $this->attachmentUploadedAt = new \DateTime();
        }
    }

    /**
     * @return \DateTime
     */
    public function getAttachmentUploadedAt(): \DateTime
    {
        return $this->attachmentUploadedAt;
    }

    /**
     * @param \DateTime $attachmentUploadedAt
     */
    public function setAttachmentUploadedAt(\DateTime $attachmentUploadedAt): void
    {
        $this->attachmentUploadedAt = $attachmentUploadedAt;
    }
}

蚂蚁映射:

vich_uploader:
    db_driver: orm

    mappings:
        tw_campus_document:
            uri_prefix:         '%paths.download.tw.campus.document%'
            upload_destination: '%kernel.project_dir%%paths.uploads.tw.campus.document%'
            namer: vich_uploader.namer_uniqid
            directory_namer: vich_uploader.namer_directory_current_date_time
            inject_on_load: true

标签: phpsymfonyvichuploaderbundle

解决方案


最近有同样的问题。我通过在我的设置器中添加它来解决它:

/**
 * @param File|null $attachmentFile
 * @throws \Exception
 */
public function setAttachmentFile(File $attachmentFile = null): self
{
    $this->attachmentFile = $attachmentFile;

    if ($attachmentFile){
        $this->attachmentUploadedAt = new \DateTime();
    }

   return $this;
}

推荐阅读