简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM