duandi1919 2015-01-03 16:50
浏览 54

symfony2 - 文件上传因表单错误而丢失

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.

  • 写回答

1条回答 默认 最新

  • dpxw17759 2015-01-03 18:14
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制