php - 提交表单而不选择任何文件时,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
解决方案
最近有同样的问题。我通过在我的设置器中添加它来解决它:
/**
* @param File|null $attachmentFile
* @throws \Exception
*/
public function setAttachmentFile(File $attachmentFile = null): self
{
$this->attachmentFile = $attachmentFile;
if ($attachmentFile){
$this->attachmentUploadedAt = new \DateTime();
}
return $this;
}
推荐阅读
- javascript - 2 个单词、非数字和一个空格的正则表达式
- python - 如何将 PyTorch DataParallel 与非张量输入一起使用
- google-cloud-platform - whoami 在 gcp 终端和本地终端上产生不同的名称
- python - 为什么实例化一个类时__init__方法被调用了两次?
- python - TypeError:尽管能够索引,但“NoneType”对象不可下标
- vue.js - Vuetify - 如何在自定义文本时包含复选框以进行 v-select
- sql-server - 如何从 ASP.NET Core 3.1 Identity 中的 IdentityUser 类添加关系?(代码优先)
- unicode - Java 不支持日文、俄文、西班牙文、法文和意大利文等多种语言的特殊字符编写 excel 文件
- unity3d - 如何找出射弹的人
- c++ - 输入验证:需要整数但输入字符串。C++