symfony2 - file upload lost upon form error - php

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:
open web/ dir of project inside browser
in name field put string "123"
set image file for file field
hit submit button
change name field to "123456"
hit submit button
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.

Related

Symfony file upload edit

Im using Symfony 3.3 and configured a file upload like in the handbook.
https://symfony.com/doc/current/controller/upload_file.html
When editing my entity in a form, after the submission of the form, the image property (like in example "Brochure") is "null", even if set it before submit.
$entity->setBrochure(new File($this->getParameter('brochures_directory').'/'.$entity->getBrochure()));
So it is not possible to decide if the user really want to remove the image or if he wants to keep it.
Did I forgot something or how is it possible to correctly handle the image?
Thank you for your answers!
Best!
It's due to HTML specifications: a file input can't be pre-filled.
A solution is to put another input submit with a different name (different than the default one). Only the button/input "clicked" by the user will be sent.
Detect it in the request; if exist, delete your file data.
And, in your view, display the original name (and/or size, type... whatever your want) near the input file.

How to restore input file type field after failling form validation in cakephp [duplicate]

Okay so here's the scenario:
User is presented with form that contains file inputs.
User submits form.
Form does not pass validation/verification for one reason or another.
User is presented form with errors highlighted.
File inputs are now blank.
Question:
Is it possible to re-populate the file inputs with the paths to the files the user originally selected. A PHP solution would be ideal, but I'm also open to JavaScript solutions as well.
I think the short answer here is no. You can't repopulate file upload fields. However, you can work around it.
If a file has been selected and the form submitted, then you've already received the file. What you can do is keep a reference to the file on disk and put that in a hidden field and show a message to indicate to the user you still have their file uploaded so it does not need to be replaced/re-uploaded. When your form gets submitted again without a file, you can check for the hidden field value and use that to get your local copy of the file they uploaded in their last attempt.
The other way to do this is to either submit the form via ajax (using either flash or the iframe method for the file upload) or to do an ajax call to validate the form first, and then only submit as normal if valid (thus no form reload, no loss of file upload field data).
I solved this problem by let user pick upload file as the last field before submit the form ^^

Editing user uploaded files?

We have a form where user can submit a message, as well as upload 1-10 files (not displayed in-line in the message, but as links at the bottom of the message).
This is all working fine so far... using two separate forms.
1 x form (MAIN/parent form) is the message details form (name. date, message..etc)
1 x form that is under this form, and if for handling the selected/uploading of user selected files.
This is done through a hidden iFrame and dedicated php script, and returns a list of uploaded file names/paths back to the MAIN/parent form (as hidden fields).. so when the MAIN/parent form is submitted.. all the data get saved to the database at one time, and the image/file uploading event/portion wont cause any loss of message data if there is timeout or other uploading error.
This is all working so far.
What I am not clear about or stuck on... is that for each message post the message 'owner/poster' can go back and edit the message..
This should include letting the user edit the files uploaded/attached to the post as well.
Which I'm not sure how I should be handling? or a nice and easy to do this?
I'm guessing I'll have to do the 'two form' approach I use for the initial posting/uploading of the message?.. but how can I pre-populate the file input fields? DO I just pre-populate the value with the file name/path form the DB.. ?

Transfering a uploaded file from page to page

My client wants to have a 3 page form. The first page allows the user to enter data including a uploaded file. the second page confirms this data. and the third page submits the data to the database and directories.
Via post, I can keep saving the data to a hidden input fields, thats no problem. My problem is the uploaded file. how do I hold that document from page to page ? I am using Cakephp but any advice would help, thanks
You can always just create the illustion that the form is utilising three different pages. Use AJAX to accept and validate/request the user confirm their submitted data. If in this view they accept it initiate a POST to submit all that data.
You really don't need three physically different files to achieve this but you can still let it appear in three stages to keep your client happy.
You just upload the file to temp directory and keep the value in hidden variables just like other form data . If form successfully submitted then the image copy to desired location other wise delete the image
You can easily fake these 3 pages using CSS. Or even 2, as "third page" is actually a server script which has nothing to do with pages in the browser.
Just make your form, then put an event on the submit button which changes divs to whatever "confirmation page" he wants. and then actually send the form using a button on this page.
that's all
An uploaded file is always held temporarily. The server env var should tell you where it is. In Ruby's rack it is stored in the params var. So I guess there is a similar params var in php which has a hash with all the necessary information.
Since the file would be uploaded on the first step, one option is to put the file's location in a hidden input field along with the rest of the data (either there, or put it in the session). With CakePHP, if your file field looks somewhat like that:
<input type="file" name="data[User][image]" id="UserImage" />
Then you will be able to capture the location through
$location = $this->data['User']['image']['tmp_name'];
Which will correspond to something like /var/tmp/xxxxxx
On the last page, if the user confirms all the data, you just use move_uploaded_file() to put the file wherever you want on the server.
move_uploaded_file($location, '/new/location');

Zend_Form_Element_File - file gets lost when form errors

I'm creating a Zend Form which includes a Zend Form Element File to allow the user to upload a file. When there is an error on the form (i.e. validation fails), when it is re-displayed for the user to correct, the file upload box is empty (so they have to click browse and select it again).
Is it possible to get it to save the file?
There is absolutely no way of a html input element to have pre-selected file (it’s a security issue). The only thing you can do is to save the file, and don’t require it to be uploaded again on form re-send.

Categories