Skip to content

Commit

Permalink
bug #5769 Fix incorrect HTTP status code for form validation errors (…
Browse files Browse the repository at this point in the history
…asispts)

This PR was merged into the 4.x branch.

Discussion
----------

Fix incorrect HTTP status code for form validation errors

This PR will fix #5768

**Before**

![000](https://github.com/EasyCorp/EasyAdminBundle/assets/79239132/75cc902e-1766-48f2-bfb5-00d4f0cc7036)

**After**

![001](https://github.com/EasyCorp/EasyAdminBundle/assets/79239132/f88999b8-f2f5-42dd-a121-af691c68d1e3)

Commits
-------

712959e Return HTTP 422 on form validation error
  • Loading branch information
javiereguiluz committed May 30, 2023
2 parents be6a5f8 + 712959e commit ef7fed4
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/EventListener/CrudResponseListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ public function onKernelView(ViewEvent $event): void

// to make parameters easier to modify, we pass around FormInterface objects
// so we must convert those values to FormView before rendering the template
$formErrorCount = 0;
foreach ($templateParameters as $paramName => $paramValue) {
if ($paramValue instanceof FormInterface) {
$templateParameters[$paramName] = $paramValue->createView();
$formErrorCount = max($formErrorCount, \count($paramValue->getErrors(true)));
}
}

$event->setResponse(new Response($this->twig->render($templatePath, $templateParameters)));
$httpCode = $formErrorCount > 0 ? Response::HTTP_UNPROCESSABLE_ENTITY : Response::HTTP_OK;
$event->setResponse(new Response($this->twig->render($templatePath, $templateParameters), $httpCode));
}
}
2 changes: 2 additions & 0 deletions tests/Controller/CategoryCrudControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public function testNew(?string $invalidCsrfToken, ?string $expectedErrorMessage
static::assertSelectorNotExists('.global-invalid-feedback');
static::assertInstanceOf(Category::class, $this->categories->findOneBy(['slug' => 'foo']));
} else {
$this->assertResponseStatusCodeSame(Response::HTTP_UNPROCESSABLE_ENTITY);
static::assertSelectorTextContains('.global-invalid-feedback', $expectedErrorMessage);
static::assertNull($this->categories->findOneBy(['slug' => 'foo']));
}
Expand Down Expand Up @@ -105,6 +106,7 @@ public function testEdit(?string $invalidCsrfToken, ?string $expectedErrorMessag
static::assertSelectorNotExists('.global-invalid-feedback');
static::assertInstanceOf(Category::class, $this->categories->findOneBy(['slug' => 'bar']));
} else {
$this->assertResponseStatusCodeSame(Response::HTTP_UNPROCESSABLE_ENTITY);
static::assertSelectorTextContains('.global-invalid-feedback', $expectedErrorMessage);
static::assertNull($this->categories->findOneBy(['slug' => 'bar']));
}
Expand Down
2 changes: 2 additions & 0 deletions tests/TestApplication/src/Entity/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

#[ORM\Entity]
class Category
Expand All @@ -14,6 +15,7 @@ class Category
#[ORM\Column(type: 'integer')]
private $id;

#[Assert\NotBlank]
#[ORM\Column(type: 'string', length: 255)]
private $name;

Expand Down

0 comments on commit ef7fed4

Please sign in to comment.