Uploading an image via form works like intended. imageName, imageSize and updatedAt fields change in database as they should. However when trying to get that uploaded image to render in a twig template it returns imageFile as null
QuestionType form class:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('question', null, array('label'=>'Klausimas'))
->add('answer', null, array('label'=>'Atsakymas'))
->add('categoryName',null, array('label'=> 'Kategorija'))
->add('imageFile', VichImageType::class, array(
'allow_delete'=>false,
'download_uri'=>false
));
}
Question entity:
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* Question
*
* @ORM\Table(name="question")
* @ORM\Entity(repositoryClass="AppBundle\Repository\QuestionRepository")
* @Vich\Uploadable
*/
class Question
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @var string
*/
private $imageName;
/**
* @Vich\UploadableField(mapping="images", fileNameProperty="imageName", size="imageSize")
* @var File
*/
private $imageFile;
/**
* @ORM\Column(type="integer")
* @var integer
*/
private $imageSize;
/**
* @ORM\Column(type="datetime")
* @var \DateTime
*/
private $updatedAt;
/**
* @param File|UploadedFile $imageName
* @throws \Exception
*/
public function setImageFile(?File $imageName = null): void
{
$this->imageFile = $imageName;
if (null !== $imageName){
$this->updatedAt = new \DateTimeImmutable();
}
}
public function getImageFile(): ?File
{
return $this->imageFile;
}
public function setImageName(?string $imageName): void
{
$this->imageName = $imageName;
}
public function getImageName(): ?string
{
return $this->imageName;
}
public function setImageSize(?int $imageSize): void
{
$this->imageSize = $imageSize;
}
public function getImageSize(): ?int
{
return $this->imageSize;
}
}
config.yml
vich_uploader:
db_driver: orm
mappings:
images:
uri_prefix: /images
upload_destination: '%kernel.project_dir%/web/images'
delete_on_update: false
twig template for editing(uploading images):
{{ form_start(edit_form) }}
{{ form_widget(edit_form, {'multipart': 'true'} ) }}
<input style="margin-top: 10px" type="submit" value="Išsaugoti" class="btn btn-success" />
{{ form_end(edit_form) }}
twig template that should display it:
<img src="{{ vich_uploader_asset(question, 'imageFile', 'AppBundle\\Entity\\Question') }}"/>
My first guess is that I added multipart for my form in the wrong place (or using incorrect syntax), but I can't find (or don't get it) the correct way to set that
Firefox says: could not load the image when I mouse over the tag in dev tools
Chrome says:
GET http://helper/images/index%20-%20Copy.jpg 404 (Not Found)
EDIT
I've found out that {{ form_start(edit_form}}
sets form to multipart already so there's no need to pass the option here {{ form_widget(edit_form, {'multipart': 'true'} ) }}
. Just gonna leave it above incase anyone else tries it as a solution