Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit turbo:submit-failed when form is rejected #319

Closed
wants to merge 1 commit into from

Conversation

terracatta
Copy link
Contributor

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 to window 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.

Comment on lines 138 to 141
if (response.clientError || response.serverError) {
this.result = { success: false, fetchResponse: response }
dispatch("turbo:submit-failed", { target: this.formElement, detail: { formSubmission: this, ...this.result } })
this.delegate.formSubmissionFailedWithResponse(this, response)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am actually not confident this code is ever called. If the fetchResponse is not a 2xx it will not be ok and therefore only the requestFailedWithResponse method will be called.

I added it here for safety, but this branch of code might not ever be called.

@en30
Copy link
Contributor

en30 commented Jul 24, 2021

After a form submission, turbo:submit-end is dispatched even when the request is failed, and it has FormSubmission as its event detail.
https://turbo.hotwired.dev/reference/events
Is turbo:submit-end not enough for your use case?

@terracatta
Copy link
Contributor Author

Is turbo:submit-end not enough for your use case?

It is, I actually didn't realize this because I think sometimes submit-end is not firing reliably in Safari. I'm going to close this as I don't think this is needed. Thanks @en30

@terracatta terracatta closed this Aug 15, 2021
@terracatta terracatta deleted the jem_add_submit_failed_event branch August 15, 2021 19:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants