2

Im using Symfony 3.3 and configured a file upload like in the handbook.

https://symfony.com/doc/current/controller/upload_file.html

When editing my entity in a form, after the submission of the form, the image property (like in example "Brochure") is "null", even if set it before submit.

$entity->setBrochure(new File($this->getParameter('brochures_directory').'/'.$entity->getBrochure()));

So it is not possible to decide if the user really want to remove the image or if he wants to keep it.

Did I forgot something or how is it possible to correctly handle the image?

Thank you for your answers!

Best!

1 Answer 1

1

It's due to HTML specifications: a file input can't be pre-filled.

A solution is to put another input submit with a different name (different than the default one). Only the button/input "clicked" by the user will be sent.

Detect it in the request; if exist, delete your file data.

And, in your view, display the original name (and/or size, type... whatever your want) near the input file.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your answer! I just added a field with the id of the file saved in the database, this is used instead of input type "file", if there is no new image uploaded! Best!
Can you please post your solution?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.