Ncls's avatar
Level 3

Return the uploaded file when validator fails

I've been sitting with this issue for quite a long time.

When you upload an image together with form data, the validator fails, returns with the input using

withInput() 

But the file upload is gone, How do you guys handle this? Thank you!

0 likes
4 replies
pmall's avatar

You cant, the file input just cant have default value.

I guess it is for security reason. What if a malicious dev put a critical system file as default value ?

Ncls's avatar
Level 3

I can imagine! So do you use a hidden field that you populate if there is an image found? Or how do you go about it?

chaibialaa's avatar

I think that if the validation is "client side" this wont happen. If the validation is server side, we can store the image path in a hidden value and return it with the validation fails state.

Demers94's avatar

I can imagine! So do you use a hidden field that you populate if there is an image found? Or how do you go about it?

I do some client-side checks, and in the rare case where the user has JS disabled or the checks don't run for some reason, then I just return to the form, repopulate every field except the image - it's an inconvenience, but it rarely happens.

As it's already been pointed out, you can't manually set the value of a file input, it's read-only, for obvious security considerations.

I'm not sure what good it would do to store the image/file path in a hidden input, if you want to upload it on your own server then the user will have to submit it via the file input any ways.

Please or to participate in this conversation.