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

Return a proper 422 HTTP status code when the form submission fails #469

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
31 changes: 31 additions & 0 deletions src/Bundle/Controller/ControllerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
Expand Down Expand Up @@ -246,6 +247,36 @@ protected function render(string $view, array $parameters = [], Response $respon
return $response;
}

/**
* Renders a view and sets the appropriate status code when a form is listed in parameters.
*
* If an invalid form is found in the list of parameters, a 422 status code is returned.
*/
protected function renderForm(string $view, array $parameters = [], Response $response = null): Response
{
if (null === $response) {
$response = new Response();
}

foreach ($parameters as $k => $v) {
if ($v instanceof FormView) {
throw new \LogicException(sprintf('Passing a FormView to "%s::renderForm()" is not supported, pass directly the form instead for parameter "%s".', get_debug_type($this), $k));
}

if (!$v instanceof FormInterface) {
continue;
}

$parameters[$k] = $v->createView();

if (200 === $response->getStatusCode() && $v->isSubmitted() && !$v->isValid()) {
$response->setStatusCode(422);
Copy link
Member

Choose a reason for hiding this comment

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

Why not using Resonse::UNPROCESSABLE_ENTITY?

does the code you wrote come from Symfony itself?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes i just copied it directly from Symfony's AbstractController.

}
}

return $this->render($view, $parameters, $response);
}

/**
* Streams a view.
*
Expand Down
8 changes: 4 additions & 4 deletions src/Bundle/Controller/ResourceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,12 @@ public function createAction(Request $request): Response
return $initializeEventResponse;
}

return $this->render($configuration->getTemplate(ResourceActions::CREATE . '.html'), [
return $this->renderForm($configuration->getTemplate(ResourceActions::CREATE . '.html'), [
'configuration' => $configuration,
'metadata' => $this->metadata,
'resource' => $newResource,
$this->metadata->getName() => $newResource,
'form' => $form->createView(),
'form' => $form,
]);
}

Expand Down Expand Up @@ -310,12 +310,12 @@ public function updateAction(Request $request): Response
return $initializeEventResponse;
}

return $this->render($configuration->getTemplate(ResourceActions::UPDATE . '.html'), [
return $this->renderForm($configuration->getTemplate(ResourceActions::UPDATE . '.html'), [
'configuration' => $configuration,
'metadata' => $this->metadata,
'resource' => $resource,
$this->metadata->getName() => $resource,
'form' => $form->createView(),
'form' => $form,
]);
}

Expand Down