rails: form_with doesn't post remote forms with empty file input on Safari
When using form_with with a single file_field (using the form helper), the form will not submit on Safari for OSX if no file was chosen. Switching to “local: true”, solves the problem.
Steps to reproduce
1 - Create a form using form_with; 2 - Add a single file input field using the file_field helper; 3 - Without selecting a file, click submit.
Expected behavior
Form is submitted in all browsers
Actual behavior
In Safari the form isn’t submitted. In Chrome, Android and Safari iOS the form is submitted.
System configuration
Rails version: 5.2.0.rc2 Ruby version: 2.4.3 and 2.5.0 OSX version: 10.13.4 Safari version: 11.1 (13605.1.33.1.2)
Relevant Code
<%= form_with(scope: :photo, url: photos_path, method: :post) do |f| %>
<%= f.file_field :file, accept: "image/gif, image/jpg, image/jpeg, image/png" %>
<%= f.submit "Save" %>
<% end %>
Demo Repository
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 1
- Comments: 16 (10 by maintainers)
Links to this issue
Commits related to this issue
- By forcing data-remote to false we can show safari works as expected Adding a comment to https://github.com/rails/rails/issues/32440 to explain the relevance of this commit. — committed to jrae/turbolinks-bug-demo by jrae 6 years ago
- Disable account setting remote form Webkit bug: https://github.com/rails/rails/issues/32440 When file input is empty, safari don't send xhr request. — committed to getcampo/campo by chloerei 6 years ago
Created (updated) workaround snippet: https://gist.github.com/ypresto/b4715b06230d4014a90eaacc3445158f
See here for detail: https://stackoverflow.com/a/49827426/1474113
Hi,
I just came across with a similar problem and found this SO. Probably it’s not a bug in Rails? Hope it would be of your help. https://stackoverflow.com/questions/49614091/safari-11-1-ajax-xhr-form-submission-fails-when-inputtype-file-is-empty
Unfortunately not. I thought of capturing a form submit event and removing the empty file input, but I couldn’t get the submit it to trigger (maybe a Turbolinks thing?). Capturing clicks on the submit button wouldn’t for me either since I sometimes trigger the submit programatically and didn’t want to refactor.
Still, if you want to try the submit button method, the closest I got was the code below (I’m not using jQuery, that’s why I’m adding the event delegation code I use below: