'VichUploaderBundle error "Expected argument of type "File", "NULL" given" when submitting the form without selecting any file
I am integrating VichUploaderBundle, and I've successfully been able to submit a file and save it, download it and edit the form resubmiting another file and replacing the original.
But when I submit the form without attaching any file, I receive the following error:
Expected argument of type "Symfony\Component\HttpFoundation\File\File", "NULL" given at property path "attachmentFile".
$form->handleRequest($request);
This is how I've added the component in the form:
$builder->add('attachmentFile', VichFileType::class, [
'label' => 'Upload file',
'allow_delete' => false,
'required' => false
]);
And this is my entity:
/**
* @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;
}
}
Ant the mapping:
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
Solution 1:[1]
Had the same issue recently. I solved it by adding this in my setter:
/**
* @param File|null $attachmentFile
* @throws \Exception
*/
public function setAttachmentFile(File $attachmentFile = null): self
{
$this->attachmentFile = $attachmentFile;
if ($attachmentFile){
$this->attachmentUploadedAt = new \DateTime();
}
return $this;
}
Solution 2:[2]
You can solve it by just modifying File to ?File in your setter:
public function setAttachmentFile(?File $attachmentFile): void
{
$this->attachmentFile = $attachmentFile;
if ($attachmentFile){
$this->attachmentUploadedAt = new \DateTime();
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Helenesh |
Solution 2 |