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

Error: Form responses must redirect to another location #22

Closed
cj opened this issue Dec 23, 2020 · 6 comments
Closed

Error: Form responses must redirect to another location #22

cj opened this issue Dec 23, 2020 · 6 comments

Comments

@cj
Copy link

cj commented Dec 23, 2020

Hi,

I am using https://viewcomponent.org/ and I am handling redirects for forms using stimulus doing the following:

  async turboSubmit(event) {
    const { success } = event.detail

    if (success && this.redirectValue) {
      Turbo.visit(this.redirectValue)
    } else {
      this.enableSubmit()
    }
  }

But it currently throws this error Error: Form responses must redirect to another location, which is due to the following code

const error = new Error("Form responses must redirect to another location")
, is there a reason this is needed?

Cheers!

@cj
Copy link
Author

cj commented Dec 23, 2020

So I ended up creating a patch for now to get it working on my end https://github.com/cj/app-template/blob/main/patches/%40hotwired%2Bturbo%2B7.0.0-beta.1.patch, but it led to what might be a bug with Turbo.visit:

CleanShot 2020-12-22 at 20 51 06

This is where I am calling Turbo.visit https://github.com/cj/app-template/blob/main/app/components/form_component/base.js#L122

@cj
Copy link
Author

cj commented Dec 23, 2020

ah so the Turbo.visit is not a bug, it was because I had render plain https://github.com/cj/app-template/blob/main/app/controllers/test_controller.rb#L11!

@danjac
Copy link

danjac commented Dec 23, 2020

I'm running into this issue as well with simple form submissions that re-render the form (i.e. the HTML page) with errors when server validation fails. Is there a reason for this behavior? Is it documented?

@sstephenson
Copy link
Contributor

We intend to allow HTTP 422 responses for form submissions inside frames, but it didn’t make the cut for the initial beta release.

For now, you can try sending a Turbo Streams response that re-renders your form with errors.

@sstephenson
Copy link
Contributor

Browsers record form submissions in history along with the verb. In a standard "postback" scenario, if you reload the page after submission, or navigate away and then back, you'll see a dialog box like this:

Screen Shot 2020-11-30 at 10 41 41 AM

The forms-must-redirect constraint is what allows us to keep the browser's back, forward, and reload buttons working after any form submission, and without showing a dialog box, since all form submissions result in a visit and all visits are idempotent GET requests.

@ollieh-m
Copy link

ollieh-m commented Dec 31, 2020

Thanks for the explanation @sstephenson. I’m probably misunderstanding something, but can’t quite see why swapping in HTML returned by the form submission (or even simulating a full page visit using the HTML response) would mess with the browser history... If the form submission is an ajax request, does the browser put the POST or PUT request in its history? If not, why would it be a problem to render HTML returned with a 200 response, in the same way 400-500 responses can now be rendered? 🙇‍♂️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants