Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

symfony2 - file upload lost upon form error

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

but I have problem handling situation when I upload file, but other field does not meet validation criteria and user is directed to fix form errors. Then when user fix problem and hit submit button, he no longer has valid image in form and missing file error is displayed.

I have prepared small example project to illustrate this behavior:

https://github.com/Jaslanin/sf_upload_form_error

Steps to reproduce this behavior:

  1. open web/ dir of project inside browser
  2. in name field put string "123"
  3. set image file for file field
  4. hit submit button
  5. change name field to "123456"
  6. hit submit button
  7. observe that form has missing file error

For now I handled this problem using steps within "How to Handle File Uploads with Doctrine" tutorial, but without using lifecyclecallbacks. I run ->upload manually and in case of error store file path inside session.

It works, but I am looking for cleaner solution for that problem.

like image 974
Jaslanin Avatar asked Feb 17 '26 09:02

Jaslanin


1 Answers

When you render the form on your page all input fields are populated with values from an entity or an array. The key point here is that you have no data to pre-populate file input field. So it renders as empty input. And when user hits submit on step 6, there is no file selected/uploaded. Your solution with a session-stored uploaded file path is proper solution in this case. I also suggest you to show to user, that some file has been already submitted when there is one in the session.

like image 159
Ziumin Avatar answered Feb 19 '26 21:02

Ziumin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!