Symfony2 - upload file on form error

I followed the tutorial: http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html

but when uploading the file, I ran into a processing issue, but the other field does not meet validation criteria and the user is submitting form error fixes. Then when the user fixes the problem and clicks the submit button, they no longer have the correct image on the form and a file error is displayed.

I have prepared a small sample project to illustrate this behavior:

https://github.com/Jaslanin/sf_upload_form_error

Steps to reproduce this behavior:

  • open the web / dir of the project inside the browser
  • put the string "123" in the name field
  • set image file for file field
  • click submit button
  • change the name field to "123456"
  • click submit button
  • Note that the form is missing a file error.

So far I have dealt with this problem using the steps in How to Process Files with Doctrine, but without using lifecyclecallbacks. I run -> upload manually and in case of the error store file path inside the session.

This works, but I am looking for a cleaner solution for this problem.

+3


source to share


1 answer


When the form is rendered on the page, all input fields are filled with values ​​from an entity or array. The key point here is that you don't have the data to pre-populate the file input field. So it appears as empty input. And when the user clicks the submit button in step 6, no file is selected / uploaded. In this case your solution with the saved uploaded file is the session path. I also suggest that you show the user that some file has already been uploaded when it is in the session.



+1


source







All Articles