Skip to content

Commit

Permalink
Return HTTP 422 code when form is invalid (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jupi007 authored Apr 25, 2024
1 parent 723b4b4 commit d95bfd8
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 7 deletions.
18 changes: 17 additions & 1 deletion Controller/AbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,25 @@ protected function renderTemplate(string $type, array $data = []): Response
Configuration::TEMPLATE
);

$response = new Response();

// Reuse logic from Symfony AbstractController.
// See: https://github.com/symfony/symfony/blob/6.3/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php#L239-L243
// See: https://github.com/symfony/symfony/blob/6.3/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php#L260-L265
foreach ($data as $k => $v) {
if ($v instanceof FormInterface) {
if ($v->isSubmitted() && !$v->isValid()) {
$response->setStatusCode(422);
}

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

return $this->render(
$template,
$data
$data,
$response
);
}

Expand Down
2 changes: 1 addition & 1 deletion Controller/CompletionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function indexAction(Request $request): Response
return $this->renderTemplate(
self::TYPE,
[
'form' => $form->createView(),
'form' => $form,
'success' => $success,
]
);
Expand Down
4 changes: 2 additions & 2 deletions Controller/PasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function forgetAction(Request $request): Response
return $this->renderTemplate(
Configuration::TYPE_PASSWORD_FORGET,
[
'form' => $form->createView(),
'form' => $form,
'success' => $success,
]
);
Expand Down Expand Up @@ -138,7 +138,7 @@ public function resetAction(Request $request, string $token): Response
return $this->renderTemplate(
Configuration::TYPE_PASSWORD_RESET,
[
'form' => $form->createView(),
'form' => $form,
'success' => $success,
]
);
Expand Down
2 changes: 1 addition & 1 deletion Controller/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function indexAction(Request $request): Response
return $this->renderTemplate(
self::TYPE,
[
'form' => $form->createView(),
'form' => $form,
'success' => $success,
]
);
Expand Down
2 changes: 1 addition & 1 deletion Controller/RegistrationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function indexAction(Request $request): Response
return $this->renderTemplate(
self::TYPE,
[
'form' => $form->createView(),
'form' => $form,
'success' => $success,
]
);
Expand Down
13 changes: 13 additions & 0 deletions Tests/Functional/Controller/ProfileControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,19 @@ public function testProfileWithoutNote(): void
$this->assertNull($user->getContact()->getNote());
}

public function testProfileInvalid(): void
{
$crawler = $this->client->request('GET', '/profile');
$this->assertHttpStatusCode(200, $this->client->getResponse());

$form = $crawler->selectButton('profile[submit]')->form([
'profile[firstName]' => null,
]);

$this->client->submit($form);
$this->assertHttpStatusCode(422, $this->client->getResponse());
}

/**
* @return array{
* 'sulu.context': SuluKernel::CONTEXT_WEBSITE,
Expand Down
24 changes: 23 additions & 1 deletion Tests/Functional/Controller/RegistrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,17 @@ public function testRegister(): void
$this->assertHttpStatusCode(302, $this->client->getResponse());
}

public function testRegisterInvalid(): void
{
$crawler = $this->client->request('GET', '/registration');

$form = $crawler->selectButton('registration[submit]')->form([
'registration[username]' => null,
]);
$this->client->submit($form);
$this->assertHttpStatusCode(422, $this->client->getResponse());
}

public function testConfirmation(): User
{
$this->testRegister();
Expand Down Expand Up @@ -180,7 +191,7 @@ public function testRegistrationBlacklistedBlocked(): void
]
);
$this->client->submit($form);
$this->assertHttpStatusCode(200, $this->client->getResponse());
$this->assertHttpStatusCode(422, $this->client->getResponse());
$content = $this->client->getResponse()->getContent();

$this->assertIsString($content);
Expand Down Expand Up @@ -344,6 +355,17 @@ public function testPasswordForget(): void
$this->assertStringStartsWith('my-new-password', $password);
}

public function testPasswordForgetInvalid(): void
{
$crawler = $this->client->request('GET', '/password-forget');

$form = $crawler->selectButton('password_forget[submit]')->form([
'password_forget[email_username]' => 'hikaru@sulu.io',
]);
$this->client->submit($form);
$this->assertHttpStatusCode(422, $this->client->getResponse());
}

/**
* Find user by username.
*/
Expand Down

0 comments on commit d95bfd8

Please sign in to comment.