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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/core/drive/form_submission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ export class FormSubmission {

requestSucceededWithResponse(request: FetchRequest, response: FetchResponse) {
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)
Comment on lines 138 to 141
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.

} else if (this.requestMustRedirect(request) && responseSucceededWithoutRedirect(response)) {
const error = new Error("Form responses must redirect to another location")
Expand All @@ -149,6 +151,7 @@ export class FormSubmission {

requestFailedWithResponse(request: FetchRequest, response: FetchResponse) {
this.result = { success: false, fetchResponse: response }
dispatch("turbo:submit-failed", { target: this.formElement, detail: { formSubmission: this, ...this.result } })
this.delegate.formSubmissionFailedWithResponse(this, response)
}

Expand Down
7 changes: 7 additions & 0 deletions src/tests/functional/form_submission_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export class FormSubmissionTests extends TurboDriveTestCase {
await this.goToLocation("/src/tests/fixtures/form.html")
await this.remote.execute(() => {
addEventListener("turbo:submit-start", () => document.documentElement.setAttribute("data-form-submitted", ""), { once: true })
addEventListener("turbo:submit-failed", () => document.documentElement.setAttribute("data-form-failed", ""), { once: true })
})
}

Expand Down Expand Up @@ -138,6 +139,7 @@ export class FormSubmissionTests extends TurboDriveTestCase {
await this.nextBody

const title = await this.querySelector("h1")
this.assert.ok(await this.formFailed)
this.assert.equal(await title.getVisibleText(), "Unprocessable Entity", "renders the response HTML")
this.assert.notOk(await this.hasSelector("#frame form.reject"), "replaces entire page")
}
Expand All @@ -147,6 +149,7 @@ export class FormSubmissionTests extends TurboDriveTestCase {
await this.nextBody

const title = await this.querySelector("h1")
this.assert.ok(await this.formFailed)
this.assert.equal(await title.getVisibleText(), "Internal Server Error", "renders the response HTML")
this.assert.notOk(await this.hasSelector("#frame form.reject"), "replaces entire page")
}
Expand Down Expand Up @@ -349,6 +352,10 @@ export class FormSubmissionTests extends TurboDriveTestCase {
this.assert.equal(await message.getVisibleText(), "Link!")
}

get formFailed(): Promise<boolean> {
return this.hasSelector("html[data-form-failed]")
}

get formSubmitted(): Promise<boolean> {
return this.hasSelector("html[data-form-submitted]")
}
Expand Down