Problem/Motivation
Ajax elements are temporarily disabled to prevent the user from changing the element’s value during an ajax request. The change event is triggered when the field loses focus. If the user hits submit without losing focus on the field (i.e., select the textfield, change its value, and then directly click the save button of the form), then the value of the field is not saved. If the field is required, the submit validation will fire and throw an error indicating that the field is empty. If the field is optional, then the form submits, but the value is silently not saved, resulting in data loss.
The reason AJAX elements are disabled is to prevent the user from changing the element's value during the request. Ideally we should make the element readonly but the readonly property isn't available for all input elements. The current implementation uses the jQuery Form plugin's options.extraData to post the data of the disabled field. This works fine if there's only one active ajax request.
Steps to reproduce
This issue can be tested using the redirect contrib module out-of-the-box, without any specific module configuration, see #3057250
Install & enable the redirect module
Steps:
- Navigate to admin/config/search/redirect/add
- First add the TO field value,
- Then click the PATH field and fill the values
- Without using tab in the keyboard or clicking outside of the field, click the save button (after filling the PATH field without clicking outside of that field, directly click the save button).
- There is a validation error saying that Path field is required.
Alternative steps
- Start at the target node
- Click Add redirect URL and you land on the pre-populated form
- Click in Path
- Enter URL
- Click Save or hit enter.
- There is a validation error saying that Path field is required.
Note: there’s a custom module in #5 that may be used to reproduce the issue, but it’s 12 years old at the time of updating this IS so it may or may not apply anymore.
Proposed resolution
Original proposed solution: Create a hidden input element for any ajax-disabled element and remove it when the ajax event finishes.
Updated proposed solution: Determine a better event type to be used to fire the ajax change.
Tl;dr, as of comment #58. The original proposed solution has been an effective workaround for several people, but it should be considered a workaround instead of a core fix because there hasn’t been a strong case for its use and it doesn’t fully address the root of the problem. According to the issue discussion, there’s wide support for disabling the submit button while ajax is running.
However, disabling submit may be out of scope here; this ticket describes behavior that may not be resolved by disabling the submit button while ajax is running since, in this case, the submit button is clicked before ajax starts.
Much longer summary of comments and discussion around the original proposed solution (as requested in #49) thru #58:
#2 & #15 are questioning the solution
not a bug as far as I can tell. And the solution is kinda hacky.
There must already exist a more standard way of dealing with this?
#9 responds to those comments:
The solution is functionally equivalent to putting the value into extraData: jquery.form.js puts all the extraData values into hidden input elements. If the jquery developers use this mechanism to add data to the submitted form, presumably it is the most reliable mechanism.
#18 & #27 & #30 & #32 are generally making the case that the submit button should not be available while ajax is running.
consider other solutions too. Such as: make the submit to wait for the blur to finish (or have the submit function perform the blur before submitting.)
working, but IMHO not in the best way… I think that after the AJAX call finishes, we should trigger the submit or give the validation error in case the field validation did not pass.
if we lose the focus for the submit button, we do not run AJAX call.
Most cases need to wait until ajax completes before submit. Image uploading i.e. So, the right solution, in my opinion, it is disabling submit buttons during ajax requests.
#19 & #21 make arguments against disabling the submit button
might also be more complicated to implement
[The proposed solution] is better than waiting for the ajax to complete. Either way we have to do validation again (obviously when submitted) so why wait for it?
#34 & #58 describe 2 separate issues here:
- We should disable the submit button during ajax requests to prevent users from submitting before we get the ajax response back.
- If clicking the submit button triggers an unrelated ajax request (as can happen e.g. if the ajax request is triggered by a changing textfield and clicking the submit button is the first thing a user does after typing in that textfield) then allow that unrelated ajax to happen first before other submit handling.
#58 argues that disabling submit while ajax is running isn’t the solution to this issue.
The problem described here is that the change event isn’t triggered until the text field loses focus, which can cause data loss because a user can submit the form without losing focus on the field, but before ajax ever runs. In other words, the form gets submitted, but ajax was never triggered, so the value isn’t saved. I think that further discussion should be around determining what type of event should be used to fire the ajax change.
Remaining tasks
Confirm the solution
Separate "disable the submit button during ajax" into a new ticket (or link to existing ticket)
Review
Commit
| Comment | File | Size | Author |
|---|
Issue fork drupal-1736308
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #1
kenneth.venken commentedAttached is a patch with the proposed solution.
Comment #2
nod_Um not a bug as far as I can tell. And the solution is kinda hacky.
Comment #3
damien tournoud commentedDon't we also disable all the submit buttons during the AJAX request?
Comment #4
kenneth.venken commented[Edited: Duplicate post]
Comment #5
kenneth.venken commentedAttached is a simple Drupal 8 test module that illustrates the behaviour. After you enable the module, visit /ajax-form-test and change some of the textfield values. The module will print the form_state['values'] as received by the ajax callback.
To reproduce:
1) Change the value of element 1 to 1
2) Change the value of element 2 to 2 during the ajax request of element 1.
3) Wait for both ajax requests to finish.
The output returned by the last ajax request will be
while the expected result would be
Comment #6
kenneth.venken commented@Damien Tournoud: Submit buttons aren't disabled during the ajax request as far as i can tell.
Comment #9
Nephele commentedI'm changing this issue into a major bug, because it causes loss of data -- specifically, user input data on a form can be lost when the form is submitted, without warning or explanation.
In particular, this issue always causes loss of data if a user simply edits any textfield that uses an ajax callback, and then clicks the Save button. Because the blur event (the trigger for the ajax processing) occurs simultaneously with the submit event, the modified field is always disabled and therefore missing when the form data is submitted to the server. Users are likely to repeat their edit, get the same result, and simply conclude that it is impossible to make edits on the website.
Re: "the solution is kinda hacky". The solution is functionally equivalent to putting the value into extraData: jquery.form.js puts all the extraData values into hidden input elements. If the jquery developers use this mechanism to add data to the submitted form, presumably it is the most reliable mechanism. Plus it is far preferable to telling users "Stop -- don't hit the Save button unless you have first made sure to click some other random location on your screen and then waited 5 to 10 seconds. Otherwise the Save button will NOT save your data."
I have tweaked the original patch and rerolled it against 8.0.x-dev. I'm also including a 7.x version of the patch, since I needed to roll it for my website.
Comment #12
Nephele commentedComment #13
Nephele commentedComment #14
abogomolov commented@Nephele thank you! Patch works perfekt.
Comment #15
lokapujyaThere must already exist a more standard way of dealing with this?
Comment #17
vadim.hirbu commentedThanks @Nephele. Patch #12 works like a charm
Comment #18
lokapujyaIt might be ok, but should consider other solutions too. Such as: make the submit to wait for the blur to finish (or have the submit function perform the blur before submitting.) Hopefully, it can also be tested.
Comment #19
berdirWas just bitten by this on a project, definitely a bug IMHO and fairly hard to identify.
Not submitting a form until ajax sounds interesting but I guess that might also be more complicated to implement?
Comment #21
_Archy_ commentedConfirming that @Nephele's patch #12 fixes the issue. I think that his fix is better than waiting for the ajax to complete. Either way we have to do validation again (obviously when submitted) so why wait for it?
Comment #23
tomz0r commentedAlso confirming Nephele's patch #12 fixes the issue.
Comment #24
Pavan B S commentedLine exceeding 80 characters, please review.
Comment #26
DedMoroz commentedRe-roll for 8.4
Comment #27
edurenye commentedThis is working, but IMHO not in the best way.
This could confuse the user, as blocking the submit this way, the user has to click again in the submit button to submit the form.
I think that after the AJAX call finishes, we should trigger the submit or give the validation error in case the field validation did not pass.
At least this happens in 8.4.
Comment #28
edurenye commentedThis is not the way, but works. Just as a workaround for the people.
Comment #29
edurenye commentedMy patch does not work either is getting the previous value :(
Comment #30
edurenye commentedMaybe we could try to do something like this somehow: https://stackoverflow.com/questions/4901899/action-on-blur-except-when-specific-element-clicked-with-jquery
So if we lose the focus for the submit button, we do not run AJAX call.
Comment #32
divined commentedIt is not a right solution. Most cases need to wait untill ajax complete before submit.
Image uploading i.e.
So, the right solution, in my opinion, it is disabling submit buttons during ajax requests,
Comment #34
jonathanshawThere are 2 separate issues:
1) We should disable the submit button during ajax requests
2) If clicking the submit button triggers an unrelated ajax request (as can happen e.g. if the ajax request is triggered by a changing textfield and clicking the submit button is the first thing a user does after typing in that textfield) then allow that have that unrelated ajax happen first before other submit handling.
Comment #37
gilsbert commentedHello my friends. This issue has more than 8 years and is a major because the data loss.
I'm sorry to put extra pressure on it specially because I'm not able to help to conclude it but are we getting a solution soon?
Comment #39
trwill commentedRerolling patch against 9.3.x
Comment #40
jeroentTried to add a test that checks if the right values are submitted.
Comment #41
jeroentComment #43
jeroentComment #44
jeroentComment #45
eric_a commentedIs this related to #2830295: Concurrent ajax submits cause user data loss?
Comment #47
sukanya.ramakrishnan commentedThis patch is a life saver. I was totally lost debugging in the backend code as to why my input value was getting lost.
After a long search, found this issue and the patch. Hope this is committed to core soon. Will save a lot of headache!!
Thanks a million!!
regards,
Sukanya
Comment #48
mohangathala commented#43 is working for me.
Comment #49
quietone commentedIt is nice to see progress on an old issue, thanks everyone!
Reviewing the issue comments I see several that are questioning the solution, #2, #15, #18, #27, #32. I see that #21 supports the solution. Based on that it doesn't seem like there is consensus here on the solution. This needs some discussion. And the result placed in the Issue Summary.
I don't see a code review in this issue. That needs to be done as well.
This should also have a fail patch, to prove the fix is working.
I am removing the Ajax tag because this is in the 'ajax component', per the issue tag guidelines.
Comment #50
mohangathala commentedComment #52
jelle_sReroll for 9.5.x
Comment #53
_utsavsharma commentedFixed CCF for #52.
Please review.
Comment #55
jelle_sReroll against 10.1.x
Comment #56
smustgrave commented#49 called for issue summary update which believe still needs to happen.
Comment #57
gauravvvv commentedFixed, CCF in #55. Attached interdiff for same. leaving as NW, as issue summary still needs to update.
Comment #58
capysara commentedI updated the IS and title based on the discussion to date and my assessment of the issue. The summary of my summary is: there isn’t sufficient support for the original proposed solution as a core fix, but there is overall support and arguments in favor of disabling the submit button.
I agree with #34 that there are 2 separate issues here.
I think that submit should be disabled so that the form can’t be submitted while the ajax change call is running and before the server has responded. If you're going all the way to the server for a change event, it's probably important that it finishes and, if that's the case, the submit button should be disabled until the form is ready to be interacted with again. Since the ajax change call response can modify the values in the form, the user should not be able to submit the form with just the values they provided in the field (e.g., entity reference fields that rely on ajax change events).
However, I don’t think that disabling the submit button addresses the reported issue and, IMO, would unnecessarily expand the scope of this ticket.
The problem described here is that the change event isn’t triggered until the text field loses focus, which can cause data loss because a user can submit the form without losing focus on the field, but before ajax ever runs. In other words, the form gets submitted, but ajax was never triggered, so the value isn’t saved.
I think that further discussion should be around determining what type of event should be used to fire the ajax change.
Comment #59
capysara commentedComment #60
mohangathala commentedComment #61
brad.bulger commentedReroll of patch in #57 for 10.3.x
Comment #62
jeroentPatch #61 does not contain all newly added files.
Comment #63
brad.bulger commentedI recreated the patch again against 11.x. It applies cleanly to 10.3.x as well.
Comment #66
volegerRoot cause of the issue: pointer devices (mouse, touchscreen) are allowed to interfere with the Ajax handler process while awaiting a response, leading to incomplete form submission and/or triggering another Ajax call with out-of-sync form data due to ongoing Ajax response expectations.
My proposed solution is to use a locking mechanism to cover two scenarios:
- Lock any form element that contains a defined Ajax handler and submit buttons from being interacted with by a pointer device during the current Ajax call processing;
- Lock any form element that contains a defined Ajax handler and submit buttons from being interacted with by a pointer device during focus on the form element with a defined Ajax handler;
My solution uses CSS property handling instead of additional event handling for a pointer device for a pointer device event locking mechanism. The reason: due to attempts to use the `disable` property or any other way of messing with event listener definitions, causing breaks in form implementations. Using the pointer-events CSS property minimizes the risk of breaking something during the lock mechanism.
Current Ajax handler implementations are working well with keyboard devices. Because the user has to navigate through the form elements to the form submit button, the Ajax handler has a chance to be executed and provide feedback to the user by rendering the throbber element while the Ajax request is being initiated.
It is currently not covered by the tests and needs an accessibility review.
Comment #67
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #68
volegerUpload the patch with MR updates
Comment #70
volegerComment #71
alphex commentedThe patch in #70 works for me.... on my laptop... but it won't deploy on pantheon...
Comment #72
alphex commentedPatch 68 works, and did deploy to pantheon.
Comment #73
volegerBe aware of the known locking issue in #68 related to the file upload widget. This widget uses a different event-triggering chain, which results in the form to which the widget belongs becoming locked.