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

Invalid forms should return a HTTP 422 code #165

Closed
Jupi007 opened this issue Mar 30, 2023 · 1 comment · Fixed by #168
Closed

Invalid forms should return a HTTP 422 code #165

Jupi007 opened this issue Mar 30, 2023 · 1 comment · Fixed by #168

Comments

@Jupi007
Copy link
Contributor

Jupi007 commented Mar 30, 2023

Q A
Bug? yes
New Feature? yes
Community Bundle Version #162
Sulu Version 2.5
Symfony version as of Sf 5.4

When a form isn't valid, Turbo expects a HTTP 422 code to be returned.
Otherwise form errors aren't handled correctly.

Actual Behavior

This bundle is using the old way of calling $this->render('...', ['form' => $form->createView()]).
This always returns a HTTP 200 code regardless of form errors.

Expected Behavior

Every form of this bundle should return a HTTP 422 when given data isn't valid.

Steps to Reproduce

Using Hotwire Turbo and this bundle, submit any form with invalid value and see the problem: no error displayed and no redirection.

Possible Solutions

As of Symfony version 5.4, you can use the renderForm() method: $this->renderForm('...', ['form' => $form]) (see: https://symfony.com/doc/5.4/forms.html#rendering-forms).
Note that, as of Sf 6.2 the renderForm() method is deprecated in favor of directly passing the FormInterface instance to render(): $this->render('...', ['form' => $form])

@alexander-schranz
Copy link
Member

alexander-schranz commented Mar 30, 2023

We can just set the 422 manually if:

if ($form->isSubmitted() && !$form->isValid()) {
     $response->setStatusCode(422);
}

return $response;

It is the same as symfony does here: https://github.com/symfony/symfony/blob/4faf38a44aaf3ec73caab18669cda309ad83366d/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php#L258-L261

or even copy that logic to:

https://github.com/sulu/SuluCommunityBundle/blob/5ed741685a5f08e2e452ff1c68160a6d85fa4939/Controller/AbstractController.php#LL109

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