Skip to content

Commit

Permalink
Return a proper 422 HTTP status code when the form submission fails
Browse files Browse the repository at this point in the history
This also brings support for Symfony UX Turbo
  • Loading branch information
belmeopmenieuwesim authored and Zales0123 committed Oct 18, 2022
1 parent 46b2938 commit 1127c3d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
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);
}
}

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

0 comments on commit 1127c3d

Please sign in to comment.