VichUploaderBundle error "Expected argument of type "File", "NULL" given" when submitting the form without selecting any file



PHP Snippet 1:

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

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

   return $this;
}

PHP Snippet 2:

public function setAttachmentFile(?File $attachmentFile): void
{
    $this->attachmentFile = $attachmentFile;
    if ($attachmentFile){
        $this->attachmentUploadedAt = new \DateTime();
    }
}