0

How to insert an image in an Symfony form file input (the image stored with entity)? I can upload and submit a file, but can't retrieve it on form (in Front I can...)

I have a form with a FileType input named imageFile:

$builder->add('imageFile', FileType::class, array('label' => 'Photo'));

enter image description here

I am using the VichUploaderBundle module to manage files

When I submit the form, the image is correctly uploaded and the filename is stored in database as expected. I can then display the picture in Front End, no problem with that.

The problem is that if I come back to the form, the picture is not attached to the field, I have to upload a picture each time I submit the form.

How can I load the picture stored and attach it to the input, so I don't need to attach it again and again?

Sorry for my broken english and also for my begginer level on Symfony! :)

Kr1
  • 1,269
  • 2
  • 24
  • 56

1 Answers1

0

In your twig try using something like this:

{% if user.file %}
<img src="{{ vich_uploader_asset(user, 'userFile') }}" />
{% endif %}

For this to work, you should override your form and add this to your Form/UserFormType:

$builder
    ->add('userFile', 'vich_image', array(
        'required' => false,
    ));

After doing so:

  1. the image will be shown when editing the form
  2. image will not be required
Torpedr
  • 137
  • 3
  • 10