Reputation: 859
I have developed a web portal using web2py. The portal has a input form that is to be filled by user. There are 5 steps in the form. At the last step there are bunch of file uploads fields.
If a user encounters form validation error after filling the form, he has to again upload the file from upload fields. Because file upload fields get reset after the form validation error. This is acceptable if user has to fill just one form. But it becomes difficult for user when he has to fill hundreds of similar forms to input data.
I want to implement a feature that persist file uploads field even after the form validation error. Is there a way to achieve this using html or php or is there something inbuilt in web2py.
Please let me know if anyone has done something like this before.
Upvotes: 1
Views: 132
Reputation: 25536
Persisting file upload fields would requiring knowing the paths to the files on the user's local machine, and browsers do not allow this as it would be a security vulnerability. There are a few alternative approaches that you could take, but web2py does not include built-in functionality to implement them.
One option would be to do an initial client-side validation (or possibly validation via Ajax if you need any server-side database lookups) before the form is submitted. You would still want to do server-side validation for security purposes, but this would at least prevent the user from submitting data that will ultimately fail validation.
Another option would be to have the user do an initial submission of all data except the files, and then upload the files only after the other data have been successfully submitted.
Finally, on the server side, when validation fails, you could store the uploaded files in a temporary location. The returned form could then show the filenames of the successfully uploaded files while also including file upload widgets in case the user wants to change any of the uploaded files. Upon successful form submission, you could then copy the temporarily stored files to the proper location. In this case, you would need some way to associate the particular form submission with the temporary files, and you might also want to run a periodic task to clean up orphaned temporary files.
Upvotes: 3