8000 [FrameworkBundle] Remove AbstractController::handleForm() · symfony/symfony@513ad95 · GitHub
[go: up one dir, main page]

Skip to content

Commit 513ad95

Browse files
committed
[FrameworkBundle] Remove AbstractController::handleForm()
1 parent 8ec7a43 commit 513ad95

File tree

4 files changed

+1
-147
lines changed

4 files changed

+1
-147
lines changed

UPGRADE-5.4.md

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
CHANGELOG
22
=========
33

4-
5.4
5-
---
6-
7-
* Deprecate `AbstractController::handleForm()`
8-
* Added `AbstractController::renderForm()` to render a form and set the appropriate HTTP status code
9-
104
5.3
115
---
126

137
* Deprecate the `session.storage` alias and `session.storage.*` services, use the `session.storage.factory` alias and `session.storage.factory.*` services instead
148
* Deprecate the `framework.session.storage_id` configuration option, use the `framework.session.storage_factory_id` configuration option instead
159
* Deprecate the `session` service and the `SessionInterface` alias, use the `Request::getSession()` or the new `RequestStack::getSession()` methods instead
16-
* Added `AbstractController::handleForm()` to handle a form and set the appropriate HTTP status code
10+
* Added `AbstractController::renderForm()` to render a form and set the appropriate HTTP status code
1711
* Added support for configuring PHP error level to log levels
1812
* Added the `dispatcher` option to `debug:event-dispatcher`
1913
* Added the `event_dispatcher.dispatcher` tag

src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -304,44 +304,6 @@ protected function stream(string $view, array $parameters = [], StreamedResponse
304304
return $response;
305305
}
306306

307-
/**
308-
* Handles a form.
309-
*
310-
* * if the form is not submitted, $render is called
311-
* * 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
312-
* * if the form is submitted and valid, $onSuccess is called, usually this method saves the data and returns a 303 HTTP redirection
313-
*
314-
* For both callables, instead of "mixed", you can use your form's data class as a type-hint for argument #2.
315-
*
316-
* @param callable(FormInterface, mixed, Request): Response $onSuccess
317-
* @param callable(FormInterface, mixed, Request): Response $render
318-
*/
319-
public function handleForm(FormInterface $form, Request $request, callable $onSuccess, callable $render): Response
320-
{
321-
trigger_deprecation('symfony/framework-bundle', '5.3', 'The method %s() is deprecated, use %s::renderForm() instead.', __METHOD__, __CLASS__);
322-
323-
$form->handleRequest($request);
324-
325-
$submitted = $form->isSubmitted();
326-
$data = $form->getData();
327-
328-
if ($isValid = $submitted && $form->isValid()) {
329-
$response = $onSuccess($form, $data, $request);
330-
} else {
331-
$response = $render($form, $data, $request);
332-
333-
if ($submitted && 200 === $response->getStatusCode()) {
334-
$response->setStatusCode(Response::HTTP_UNPROCESSABLE_ENTITY);
335-
}
336-
}
337-
338-
if (!$response instanceof Response) {
339-
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)));
340-
}
341-
342-
return $response;
343-
}
344-
345307
/**
346308
* Returns a NotFoundHttpException.
347309
*

src/Symfony/Bundle/FrameworkBundle/Tests/Controller/AbstractControllerTest.php

Lines changed: 0 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
use Symfony\Component\Form\FormConfigInterface;
2424
use Symfony\Component\Form\FormError;
2525
use Symfony\Component\Form\FormFactoryInterface;
26-
use Symfony\Component\Form\FormInterface;
2726
use Symfony\Component\HttpFoundation\BinaryFileResponse;
2827
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
2928
use Symfony\Component\HttpFoundation\File\File;
@@ -463,100 +462,6 @@ public function testStreamTwig()
463462
$this->assertInstanceOf(StreamedResponse::class, $controller->stream('foo'));
464463
}
465464

466-
/**
467-
* @group legacy
468-
*/
469-
public function testHandleFormNotSubmitted()
470-
{
471-
$form = $this->createMock(FormInterface::class);
472-
$form->expects($this->once())->method('isSubmitted')->willReturn(false);
473-
474-
$controller = $this->createController();
475-
$response = $controller->handleForm(
476-
$form,
477-
Request::create('https://example.com'),
478-
function (FormInterface $form, $data, Request $request): Response {
479-
return new RedirectResponse('https://example.com/redir', Response::HTTP_SEE_OTHER);
480-
},
481-
function (FormInterface $form, $data, Request $request): Response {
482-
return new Response('rendered');
483-
}
484-
);
485-
486-
$this->assertTrue($response->isSuccessful());
487-
$this->assertSame('rendered', $response->getContent());
488-
}
489-
490-
/**
491-
* @group legacy
492-
*/
493-
public function testHandleFormInvalid()
494-
{
495-
$form = $this->createMock(FormInterface::class);
496-
$form->expects($this->once())->method('isSubmitted')->willReturn(true);
497-
$form->expects($this->once())->method('isValid')->willReturn(false);
498-
499-
$controller = $this->createController();
500-
$response = $controller->handleForm(
501-
$form,
502-
Request::create('https://example.com'),
503-
function (FormInterface $form, $data, Request $request): Response {
504-
return new RedirectResponse('https://example.com/redir', Response::HTTP_SEE_OTHER);
505-
},
506-
function (FormInterface $form, $data, Request $request): Response {
507-
return new Response('rendered');
508-
}
509-
);
510-
511-
$this->assertSame(Response::HTTP_UNPROCESSABLE_ENTITY, $response->getStatusCode());
512-
$this->assertSame('rendered', $response->getContent());
513-
}
514-
515-
/**
516-
* @group legacy
517-
*/
518-
public function testHandleFormValid()
519-
{
520-
$form = $this->createMock(FormInterface::class);
521-
$form->expects($this->once())->method('isSubmitted')->willReturn(true);
522-
$form->expects($this->once())->method('isValid')->willReturn(true);
523-
524-
$controller = $this->createController();
525-
$response = $controller->handleForm(
526-
$form,
527-
Request::create('https://example.com'),
528-
function (FormInterface $form, $data, Request $request): Response {
529-
return new RedirectResponse('https://example.com/redir', Response::HTTP_SEE_OTHER);
530-
},
531-
function (FormInterface $form, $data, Request $request): Response {
532-
return new Response('rendered');
533-
}
534-
);
535-
536-
$this->assertInstanceOf(RedirectResponse::class, $response);
537-
$this->assertSame(Response::HTTP_SEE_OTHER, $response->getStatusCode());
538-
$this->assertSame('https://example.com/redir', $response->getTargetUrl());
539-
}
540-
541-
public function testHandleFormTypeError()
542-
{
543-
$form = $this->createMock(FormInterface::class);
544-
$form->expects($this->once())->method('isSubmitted')->willReturn(true);
545-
$form->expects($this->once())->method('isValid')->willReturn(true);
546-
547-
$controller = $this->createController();
548-
549-
$this->expectException(\TypeError::class);
550-
$this->expectExceptionMessage('The "$onSuccess" callable passed to "Symfony\Bundle\FrameworkBundle\Tests\Controller\TestAbstractController::handleForm()" must return a Response, "string" returned.');
551-
552-
$response = $controller->handleForm(
553-
$form,
554-
Request::create('https://example.com'),
555-
function () { return 'abc'; },
556-
function () { return 'abc'; }
557-
);
558-
}
559-
560465
public function testRedirectToRoute()
561466
{
562467
$router = $this->createMock(RouterInterface::class);

0 commit comments

Comments
 (0)
0