Emit turbo:submit-failed
when form is rejected
#319
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR adds a new event to the Turbo Drive form submission lifecycle called
turbo:submit-failed
.This event is dispatched when a form is rejected by the server or the server throws an error.Why a new event?
In a situation where a form submission is successful, Turbo Drive will follow the redirect and kick off a
turbo:visit
event. A lot of JS libs explicitly look out for this event to correct their state with the DOM is redrawn. One example is Intercom. In this situation, everything works as expected.But when a form is rejected by the server, new HTML is rendered in the DOM, but no
turbo:visit
event is ever fired. This means external scripts that may need to redraw HTML elements in a situation where they may already have state attached towindow
cannot do so.I think the event could also be useful in other JS contexts (ex: like sending a report to an error monitoring service, or scrolling the window to a specific position), but I wanted to be honest about my need for it.