8000 [FrameworkBundle] improve AbstractController::handleForm() by nicolas-grekas · Pull Request #41181 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[FrameworkBundle] improve AbstractController::handleForm() #41181

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

Merged
merged 1 commit into from
May 12, 2021
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -297,23 +297,30 @@ protected function stream(string $view, array $parameters = [], StreamedResponse
* * if the form is submitted but invalid, $render is called and a 422 HTTP status code is set if the current status hasn't been customized
* * if the form is submitted and valid, $onSuccess is called, usually this method saves the data and returns a 303 HTTP redirection
*
* @param callable(FormInterface, mixed): Response $onSuccess
* @param callable(FormInterface, mixed): Response $render
* For both callables, instead of "mixed", you can use your form's data class as a type-hint for argument #2.
*
* @param callable(FormInterface, mixed, Request): Response $onSuccess
* @param callable(FormInterface, mixed, Request): Response $render
*/
public function handleForm(FormInterface $form, Request $request, callable $onSuccess, callable $render): Response
{
$form->handleRequest($request);

$submitted = $form->isSubmitted();

$data = $form->getData();
if ($submitted && $form->isValid()) {
return $onSuccess($form, $data);

if ($isValid = $submitted && $form->isValid()) {
$response = $onSuccess($form, $data, $request);
} else {
$response = $render($form, $data, $request);

if ($submitted && 200 === $response->getStatusCode()) {
$response->setStatusCode(Response::HTTP_UNPROCESSABLE_ENTITY);
}
}

$response = $render($form, $data);
if ($submitted && 200 === $response->getStatusCode()) {
$response->setStatusCode(Response::HTTP_UNPROCESSABLE_ENTITY);
if (!$response instanceof Response) {
throw new \TypeError(sprintf('The "%s" callable passed to "%s::handleForm()" must return a Response, "%s" returned.', $isValid ? '$onSuccess' : '$render', get_debug_type($this), get_debug_type($response)));
}

return $response;
Expand Down
8000
Original file line number Diff line number Diff line change
Expand Up @@ -433,10 +433,10 @@ public function testHandleFormNotSubmitted()
$response = $controller->handleForm(
$form,
Request::create('https://example.com'),
function (FormInterface $form, $data): Response {
function (FormInterface $form, $data, Request $request): Response {
return new RedirectResponse('https://example.com/redir', Response::HTTP_SEE_OTHER);
},
function (FormInterface $form, $data): Response {
function (FormInterface $form, $data, Request $request): Response {
return new Response('rendered');
}
);
Expand All @@ -455,10 +455,10 @@ public function testHandleFormInvalid()
$response = $controller->handleForm(
$form,
Request::create('https://example.com'),
function (FormInterface $form): Response {
function (FormInterface $form, $data, Request $request): Response {
return new RedirectResponse('https://example.com/redir', Response::HTTP_SEE_OTHER);
},
function (FormInterface $form): Response {
function (FormInterface $form, $data, Request $request): Response {
return new Response('rendered');
}
);
Expand All @@ -477,10 +477,10 @@ public function testHandleFormValid()
$response = $controller->handleForm(
$form,
Request::create('https://example.com'),
function (FormInterface $form): Response {
function (FormInterface $form, $data, Request $request): Response {
return new RedirectResponse('https://example.com/redir', Response::HTTP_SEE_OTHER);
},
function (FormInterface $form): Response {
function (FormInterface $form, $data, Request $request): Response {
return new Response('rendered');
}
);
Expand All @@ -490,6 +490,25 @@ function (FormInterface $form): Response {
$this->assertSame('https://example.com/redir', $response->getTargetUrl());
}

public function testHandleFormTypeError()
{
$form = $this->createMock(FormInterface::class);
$form->expects($this->once())->method('isSubmitted')->willReturn(true);
$form->expects($this->once())->method('isValid')->willReturn(true);

$controller = $this->createController();

$this->expectException(\TypeError::class);
$this->expectExceptionMessage('The "$onSuccess" callable passed to "Symfony\Bundle\FrameworkBundle\Tests\Controller\TestAbstractController::handleForm()" must return a Response, "string" returned.');

$response = $controller->handleForm(
$form,
Request::create('https://example.com'),
function () { return 'abc'; },
function () { return 'abc'; }
);
}

public function testRedirectToRoute()
{
$router = $this->createMock(RouterInterface::class);
Expand Down
0