Closed paulbernhard closed 4 years ago
We'll need to dive deeper into possibly adding support for file inputs.
For the time being, I think the best course of action is to consider file inputs as unsupported and mark them as data-reflex-permanent
so you don't lose any state when reflexes are triggered.
Hi and thanks for the response, i am looking forward to hear and see more of StimulusReflex.
It might not be a solution to the actual "problem" of Stimulus Reflex not supporting file inputs, but for the people using the Shrine gem to handle file uploads, there might be a "frontend workaround":
Shrine provides a Upload Endpoint plugin in order to use JS upload libraries (e.g. Uppy). This endpoint accepts a post request with the uploaded file and returns a JSON response.
Using a JS uploader and the upload endpoint we can avoid using a input[type="file"]
, instead populate a input[type="hidden"]
with the necessary JSON data and pass it to a reflex.
An example implementation of Uppy with Shrine and the Upload endpoint can be found here: Better File Uploads with Shrine: Direct Uploads.
Hey Paul! (Are you on Discord? If not, please consider joining us!)
This sounds like a potentially viable workaround, at least for Shrine users. If you are able to try this out, verify it and help extract the important bits, I'd love to work with you to add an example of this into the documentation because you're probably not the last person to ask about uploads.
Yeah running into the same issue here! I'll try shrine or some other things. Following thread
Hey @leastbad, i am sorry for the delayed answer. I already made a first tryout which worked and i could work out an example. Right now i am too occupied to do this but i will have to work further with async file uploads in an upcoming project and i am planning to hook them to Stimulus Reflex. For anybody in need right now, i think the approach described above (https://github.com/hopsoft/stimulus_reflex/issues/277#issuecomment-661726137) is a good start.
As soon as i'd have something to share, i'll post it here as i am not on Discord.
Hi ( @leastbad ), following the question to circumvent the problem of using input type="file"
fields in order to submit files through forms using StimulusReflex, i just wrote a small dummy app which makes use of StimulusReflex in combination with Shrine which can be found here: https://gitlab.com/swrs/stimulus-reflex-shrine.
The application is obviously rough and uses Shrine's Upload endpoint together with the frontend Uppy uploader, submitting a form upon file upload. I included some file validations for mime-types (image/jpeg image/jpg image/png image/gif application/pdf
) and file size (< 3MB
) in order to test error behaviour. As the form is submitted after every file upload, it is possible to do batch uploads of multiple files.
The docs mention: if you're thinking of replacing UJS remote forms with StimulusReflex form handling without a specific reason for doing so... just stick with Rails! which seems to be quite true, but for file uploads it could make sense to pass a form through a reflex in order to facilitate validations, a file thumb partial, … .
Some short notes on how it works:
– uploader_controller.js
initializes the Uppy dashboard by clicking data-target="uploader.button"
.
– Upon selecting a file, Uppy uploads files to Shrine's upload endpoint which responds with the data to populate the record's file
attribute.
– Upon a successful upload, the response is stored in an input type="hidden"
and the form is submitted via the reflex Upload#submit
.
Bug Report
First of all, thanks for building this, Reflex looks very (very) promising to me.
Describe the bug
I am building a form based on the Working with HTML Forms from the StimulusReflex docs. This form should accept a file input on the parameter
file_data
for further file uploading. The form triggers a reflex calledsubmit
inUploadReflex
which should take care of building or updating, and saving the record.When the reflex receives the
params
from the form, thefile_data
parameter which was set by afile_field
is always empty. When thefile_data
parameter is provided by atext_field
instead offile_field
, thefile_data
in the reflex'params
is not empty. Equally, when the form is submitted to the corresponding controller without using a reflex, thefile_data
parameter inparams
corresponds to thefile_field
input.Thus it seems, the reflex is ignoring parameters provided by a file input.
To Reproduce
When the form is submitted using
submit->UploadReflex#submit
the methodupload_params
inUploadReflex#submit
returns:When the same form is submitted without the reflex, the method
upload_params
inUploadsController#create
returns:Expected behavior
The
upload_params
inUploadReflex#submit
should read thefile_data
parameter provided by ainput[type="file"]
.Versions
StimulusReflex
External tools
Browser