P
P
Pr00f2020-06-13 09:00:27
symfony
Pr00f, 2020-06-13 09:00:27

Form validation, how to save uploaded files?

The Symfony project has a large form that has a few more fields for uploading files. Currently, files are only uploaded if the form is filled out correctly and successfully validated. And if there are any errors in the form, then the uploaded files are lost and they have to be uploaded again.
Making a separate file manager for uploading is not suitable - you can upload extra files, there are difficulties in editing / updating records. AJAX form validation will partially solve the problem - if one of the files does not pass, you will have to upload all the files again.
Maybe there is some way that all downloaded files can be saved?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alexalexes, 2020-06-13
@Pr00f

Making a separate file manager for uploading is not suitable - you can upload extra files, there are difficulties in editing / updating records. AJAX form validation will partially solve the problem - if one of the files does not pass, you will have to upload all the files again.

You just accumulated technical debt in the form of a full-fledged file manager implementation.
Which should send each file via ajax to the server immediately after the user selects files. The server at this time adds files to the user's temporary directory, giving his temporary hash name as an ajax response. When submitting the form, you no longer send the contents of input[type=file], but send a list of hash names of successfully uploaded files, and on the server side, if all fields are valid and the files are valid (they can be verified), then simply copy the files from the temporary directory to a permanent one (or to the database, or to the cloud, or another cloud directory, depending on where the main storage is). If some kind of check is unsuccessful, then you can easily return back to the client its filled fields, and a list of downloaded files, indicating which file has which error. On the client side, you can restore both the fields and the list of files in the manager.
If you do not want to fully solve technical debt, then you can do it in an easier way.
After the user selects the files, read their contents with the FileReader object and base64 encoded into the hidden input[type=hidden] fields (when finished, clear the input[type=file] so that the form is only loaded with binary data from the hidden fields), this will allow you to send the list of files along with the form submission, and restore the input[type=hidden] fields when an error occurs, while returning the form data back to the client for correction.
This approach will increase the time of sending and receiving the form, it will not free you from the fact that you need to draw the state of the list of files on the form, it will only free you from using ajax.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question