3

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 Answer 1

1

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.

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

Comments

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.