8000 [FrameworkBundle] Introduce AbstractController::renderForm() · symfony/symfony@7852349 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7852349

Browse files
committed
[FrameworkBundle] Introduce AbstractController::renderForm()
1 parent 094b507 commit 7852349

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

UPGRADE-5.4.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
UPGRADE FROM 5.3 to 5.4
2+
=======================
3+
4+
FrameworkBundle
5+
---------------
6+
7+
* Deprecate `AbstractController::handleForm()`, use `AbstractController::handleForm()` instead

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
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+
410
5.3
511
---
612

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,20 @@ protected function render(string $view, array $parameters = [], Response $respon
266266
return $response;
267267
}
268268

269+
/**
270+
* Renders a view for a form.
271+
*/
272+
protected function renderForm(string $view, FormInterface $form, array $parameters = [], Response $response = null): Response
273+
{
274+
$response = $this->render($view, $parameters, $response);
275+
276+
if ($form->isSubmitted() && !$form->isValid()) {
277+
$response->setStatusCode(422);
278+
}
279+
280+
return $response;
281+
}
282+
269283
/**
270284
* Streams a view.
271285
*/
@@ -302,6 +316,8 @@ protected function stream(string $view, array $parameters = [], StreamedResponse
302316
*/
303317
public function handleForm(FormInterface $form, Request $request, callable $onSuccess, callable $render): Response
304318
{
319+
trigger_deprecation('symfony/framework-bundle', '5.3', 'The method %s() is deprecated, use %s::renderForm() instead.', __METHOD__, __CLASS__);
320+
305321
$form->handleRequest($request);
306322

307323
$submitted = $form->isSubmitted();

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Symfony\Component\Form\Form;
2222
use Symfony\Component\Form\FormBuilderInterface;
2323
use Symfony\Component\Form\FormConfigInterface;
24+
use Symfony\Component\Form\FormError;
2425
use Symfony\Component\Form\FormFactoryInterface;
2526
use Symfony\Component\Form\FormInterface;
2627
use Symfony\Component\HttpFoundation\BinaryFileResponse;
@@ -411,6 +412,44 @@ public function testRenderTwig()
411412
$this->assertEquals('bar', $controller->render('foo')->getContent());
412413
}
413414

415+
public function testRenderFormNew()
416+
{
417+
$twig = $this->createMock(Environment::class);
418+
$twig->expects($this->once())->method('render')->willReturn('bar');
419+
420+
$container = new Container();
421+
$container->set('twig', $twig);
422+
423+
$controller = $this->createController();
424+
$controller->setContainer($container);
425+
$form = new Form($this->createMock(FormConfigInterface::class));
426+
427+
$response = $controller->renderForm('foo', $form);
428+
$this->assertEquals(200, $response->getStatusCode());
429+
$this->assertEquals('bar', $response->getContent());
430+
}
431+
432+
public function testRenderFormSubmittedAndInvalid()
433+
{
434+
$twig = $this->createMock(Environment::class);
435+
$twig->expects($this->once())->method('render')->willReturn('bar');
436+
437+
$container = new Container();
438+
$container->set('twig', $twig);
439+
440+
$controller = $this->createController();
441+
$controller->setContainer($container);
442+
$form = new Form($this->createMock(FormConfigInterface::class));
443+
$formReflection = new \ReflectionProperty($form, 'submitted');
444+
$formReflection->setAccessible(true);
445+
$formReflection->setValue($form, true);
446+
$form->addError(new FormError('message'));
447+
448+
$response = $controller->renderForm('foo', $form);
449+
$this->assertEquals(422, $response->getStatusCode());
450+
$this->assertEquals('bar', $response->getContent());
451+
}
452+
414453
public function testStreamTwig()
415454
{
416455
$twig = $this->createMock(Environment::class);
@@ -424,6 +463,9 @@ public function testStreamTwig()
424463
$this->assertInstanceOf(StreamedResponse::class, $controller->stream('foo'));
425464
}
426465

466+
/**
467+
* @group legacy
468+
*/
427469
public function testHandleFormNotSubmitted()
428470
{
429471
$form = $this->createMock(FormInterface::class);
@@ -445,6 +487,9 @@ function (FormInterface $form, $data): Response {
445487
$this->assertSame('rendered', $response->getContent());
446488
}
447489

490+
/**
491+
* @group legacy
492+
*/
448493
public function testHandleFormInvalid()
449494
{
450495
$form = $this->createMock(FormInterface::class);
@@ -467,6 +512,9 @@ function (FormInterface $form): Response {
467512
$this->assertSame('rendered', $response->getContent());
468513
}
469514

515+
/**
516+
* @group legacy
517+
*/
470518
public function testHandleFormValid()
471519
{
472520
$form = $this->createMock(FormInterface::class);

0 commit comments

Comments
 (0)
0