Skip to content

Use 422 instead of 200 as the status code for form submission error responses#41026

Merged
kaspth merged 1 commit intorails:masterfrom
santib:use-4xx-for-validations-errors-responses
Jan 10, 2021
Merged

Use 422 instead of 200 as the status code for form submission error responses#41026
kaspth merged 1 commit intorails:masterfrom
santib:use-4xx-for-validations-errors-responses

Conversation

@santib
Copy link
Copy Markdown
Contributor

@santib santib commented Jan 6, 2021

Summary

Turbo requires form submissions to either redirect or respond with a 4XX or 5XX status code. This PRs adjust the scaffold generator and documentation so Rails is now in alignment with Turbo.

More info:

@santib santib force-pushed the use-4xx-for-validations-errors-responses branch from f1abb62 to e83d7dc Compare January 6, 2021 03:10
@kaspth kaspth merged commit 2afc905 into rails:master Jan 10, 2021
@kaspth
Copy link
Copy Markdown
Contributor

kaspth commented Jan 10, 2021

Thanks @santib 🙌

@letiesperon
Copy link
Copy Markdown

Amazing!

fabpot added a commit to symfony/symfony that referenced this pull request Jan 17, 2021
…appropriate HTTP status code (dunglas)

This PR was squashed before being merged into the 5.3-dev branch.

Discussion
----------

[FrameworkBundle] Add renderForm() helper setting the appropriate HTTP status code

| Q             | A
| ------------- | ---
| Branch?       | 5.x
| Bug fix?      | no
| New feature?  | yes <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       | n/a
| License       | MIT
| Doc PR        | todo

A 422 HTTP status code should be returned after the submission of an invalid form. Some libraries including [Turbo](hotwired/turbo#39) rely on this behavior and will not display the updated form (containing errors) unless this status code is present.

Rails also [recently switched to this behavior ](rails/rails#41026) by default for the same reason.

I propose to introduce a new helper method rendering the form and setting the appropriate status code. It makes the code cleaner:

```php
// src/Controller/TaskController.php

// ...
use Symfony\Component\HttpFoundation\Request;

class TaskController extends AbstractController
{
    public function new(Request $request): Response
    {
        $task = new Task();
        $form = $this->createForm(TaskType::class, $task);

        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            $task = $form->getData();
            // ...

            return $this->redirectToRoute('task_success');
        }

        return $this->renderForm('task/new.html.twig', $form);
    }
}
```

Commits
-------

4c77e50 [FrameworkBundle] Add renderForm() helper setting the appropriate HTTP status code
ghiculescu added a commit to ghiculescu/devise that referenced this pull request Feb 2, 2021
Devise `FailureApp` now returns a `422` status when running the `recall` flow. This, combined with heartcombo/responders#223, enables Devise to work correctly with the [new Rails defaults](rails/rails#41026) for form errors, as well as with new libraries like Turbo.

By default, the `recall` flow only runs on the [`SessionsController`](https://github.com/heartcombo/devise/blob/v4.7.3/app/controllers/devise/sessions_controller.rb#L48).
ghiculescu added a commit to ghiculescu/devise that referenced this pull request Feb 3, 2021
Devise `FailureApp` now returns a `422` status when running the `recall` flow. This, combined with heartcombo/responders#223, enables Devise to work correctly with the [new Rails defaults](rails/rails#41026) for form errors, as well as with new libraries like Turbo.

By default, the `recall` flow only runs on the [`SessionsController`](https://github.com/heartcombo/devise/blob/v4.7.3/app/controllers/devise/sessions_controller.rb#L48).
ghiculescu added a commit to ghiculescu/devise that referenced this pull request Feb 3, 2021
Devise `FailureApp` now returns a `422` status when running the `recall` flow. This, combined with heartcombo/responders#223, enables Devise to work correctly with the [new Rails defaults](rails/rails#41026) for form errors, as well as with new libraries like Turbo.

By default, the `recall` flow only runs on the [`SessionsController`](https://github.com/heartcombo/devise/blob/v4.7.3/app/controllers/devise/sessions_controller.rb#L48).
ghiculescu added a commit to ghiculescu/devise that referenced this pull request Feb 3, 2021
Devise `FailureApp` now returns a `422` status when running the `recall` flow. This, combined with heartcombo/responders#223, enables Devise to work correctly with the [new Rails defaults](rails/rails#41026) for form errors, as well as with new libraries like Turbo.

By default, the `recall` flow only runs on the [`SessionsController`](https://github.com/heartcombo/devise/blob/v4.7.3/app/controllers/devise/sessions_controller.rb#L48).
ghiculescu added a commit to ghiculescu/devise that referenced this pull request Feb 3, 2021
Devise `FailureApp` now returns a `422` status when running the `recall` flow. This, combined with heartcombo/responders#223, enables Devise to work correctly with the [new Rails defaults](rails/rails#41026) for form errors, as well as with new libraries like Turbo.

By default, the `recall` flow only runs on the [`SessionsController`](https://github.com/heartcombo/devise/blob/v4.7.3/app/controllers/devise/sessions_controller.rb#L48).
thomasklemm pushed a commit to thomasklemm/devise that referenced this pull request Mar 31, 2021
Devise `FailureApp` now returns a `422` status when running the `recall` flow. This, combined with heartcombo/responders#223, enables Devise to work correctly with the [new Rails defaults](rails/rails#41026) for form errors, as well as with new libraries like Turbo.

By default, the `recall` flow only runs on the [`SessionsController`](https://github.com/heartcombo/devise/blob/v4.7.3/app/controllers/devise/sessions_controller.rb#L48).
yrashk pushed a commit to HackerIntro/devise that referenced this pull request Jul 24, 2021
Devise `FailureApp` now returns a `422` status when running the `recall` flow. This, combined with heartcombo/responders#223, enables Devise to work correctly with the [new Rails defaults](rails/rails#41026) for form errors, as well as with new libraries like Turbo.

By default, the `recall` flow only runs on the [`SessionsController`](https://github.com/heartcombo/devise/blob/v4.7.3/app/controllers/devise/sessions_controller.rb#L48).
marcelolx pushed a commit to rails/request.js that referenced this pull request Aug 12, 2021
This PR adds a convenient helper to check for unprocessableEntity response status.

I bet we don't want to add a helper for each status code but this is motivated by the fact that Rails now returns 422 status code by default for form submission with error rails/rails#41026

So, we can legitimately expect doing this kind of check more often.
miharekar pushed a commit to miharekar/devise that referenced this pull request Sep 4, 2021
Devise `FailureApp` now returns a `422` status when running the `recall` flow. This, combined with heartcombo/responders#223, enables Devise to work correctly with the [new Rails defaults](rails/rails#41026) for form errors, as well as with new libraries like Turbo.

By default, the `recall` flow only runs on the [`SessionsController`](https://github.com/heartcombo/devise/blob/v4.7.3/app/controllers/devise/sessions_controller.rb#L48).
liaden pushed a commit to liaden/devise that referenced this pull request Sep 28, 2021
Devise `FailureApp` now returns a `422` status when running the `recall` flow. This, combined with heartcombo/responders#223, enables Devise to work correctly with the [new Rails defaults](rails/rails#41026) for form errors, as well as with new libraries like Turbo.

By default, the `recall` flow only runs on the [`SessionsController`](https://github.com/heartcombo/devise/blob/v4.7.3/app/controllers/devise/sessions_controller.rb#L48).
miharekar pushed a commit to miharekar/devise that referenced this pull request Oct 6, 2021
Devise `FailureApp` now returns a `422` status when running the `recall` flow. This, combined with heartcombo/responders#223, enables Devise to work correctly with the [new Rails defaults](rails/rails#41026) for form errors, as well as with new libraries like Turbo.

By default, the `recall` flow only runs on the [`SessionsController`](https://github.com/heartcombo/devise/blob/v4.7.3/app/controllers/devise/sessions_controller.rb#L48).
ghiculescu added a commit to ghiculescu/devise that referenced this pull request Oct 15, 2021
Devise `FailureApp` now returns a `422` status when running the `recall` flow. This, combined with heartcombo/responders#223, enables Devise to work correctly with the [new Rails defaults](rails/rails#41026) for form errors, as well as with new libraries like Turbo.

By default, the `recall` flow only runs on the [`SessionsController`](https://github.com/heartcombo/devise/blob/v4.7.3/app/controllers/devise/sessions_controller.rb#L48).
baarkerlounger added a commit to baarkerlounger/devise that referenced this pull request Jan 12, 2022
Devise `FailureApp` now returns a `422` status when running the `recall` 
flow. This, combined with heartcombo/responders#223, enables Devise to 
work correctly with the [new Rails defaults](rails/rails#41026) for form 
errors, as well as with new libraries like Turbo.

By default, the `recall` flow only runs on the 
[`SessionsController`](https://github.com/heartcombo/devise/blob/v4.7.3/app/controllers/devise/sessions_controller.rb#L48).
yrashk pushed a commit to HackerIntro/devise that referenced this pull request Feb 16, 2022
Devise `FailureApp` now returns a `422` status when running the `recall` flow. This, combined with heartcombo/responders#223, enables Devise to work correctly with the [new Rails defaults](rails/rails#41026) for form errors, as well as with new libraries like Turbo.

By default, the `recall` flow only runs on the [`SessionsController`](https://github.com/heartcombo/devise/blob/v4.7.3/app/controllers/devise/sessions_controller.rb#L48).
baarkerlounger added a commit to baarkerlounger/devise that referenced this pull request Feb 28, 2022
Devise `FailureApp` now returns a `422` status when running the `recall` 
flow. This, combined with heartcombo/responders#223, enables Devise to 
work correctly with the [new Rails defaults](rails/rails#41026) for form 
errors, as well as with new libraries like Turbo.

By default, the `recall` flow only runs on the 
[`SessionsController`](https://github.com/heartcombo/devise/blob/v4.7.3/app/controllers/devise/sessions_controller.rb#L48).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants