I have a form to edit a model with
images attributes. Name is a string (min 3 chars), images is an array of string.
I’m using the example code from the official docs to implement the multi file upload using
live_file_input to populate the images field.
When clicking on the submit button the
save event handler is triggered, consuming files and copying them to the
If the form has no errors, the changeset validations are correct and the model is persisted.
If the form has an error (for example when the name is less than 3 chars):
- the files are consumed
- then the changeset validations are executed and the changeset is marked invalid
- then the invalid changeset is re-rendered again, so that the user can fix the errors, but by now the files selected are gone (as they are already consumed).
What I have learned:
live_file_inputdoes a preflight request when submitting that, if there is an error in the live_file_input, it does nothing and the submit is aborted until errors are fixed. This doesn’t take into account any other error on the form.
- I might do the changeset validation on the
savehandler and only then try to consume the files and save the model, but it feels like the
validatehandler is the place where this logic should be, not the
- Maybe a boolean option might be useful in the
allow_uploadsto enable/disable saving. We could use it to control the
live_file_inputsubmit behavior from the
live_viewside by forcing the preflight checks to abort the submit if it is set to false. Something like:
# on mount/update: is_valid = true .... |> allow_upload(:images, accept: ~w(.png .jpg .jpeg), max_entries: @max_entries, max_file_size: @max_file_size, valid: is_valid )} # on validate: is_valid = some && business && validation ... |> allow_upload(:images, valid: is_valid )}
I have a working example that shows this behavior here . Just go to localhost:4000/products, add two images and put a name with one char to see the validation error on the name field and submit. The page reloads but the files are gone.
Is there a way for the
save operation to honor both form errors and live_file_input errors before attempting the consumption of files and saving the model?
Thanks in advance for your insights