Skip to content

Commit 2372e6b

Browse files
[FrameworkBundle] Make AbstractController::render() able to deal with forms and deprecate renderForm()
1 parent 473b5b2 commit 2372e6b

File tree

3 files changed

+78
-25
lines changed

3 files changed

+78
-25
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ CHANGELOG
44
6.2
55
---
66

7-
* Deprecate the `Symfony\Component\Serializer\Normalizer\ObjectNormalizer` and
8-
`Symfony\Component\Serializer\Normalizer\PropertyNormalizer` autowiring aliases, type-hint against
9-
`Symfony\Component\Serializer\Normalizer\NormalizerInterface` or implement `NormalizerAwareInterface` instead
107
* Add option `framework.catch_all_throwables` to allow `Symfony\Component\HttpKernel\HttpKernel` to catch all kinds of `Throwable`
8+
* Make `AbstractController::render()` able to deal with forms and deprecate `renderForm()`
9+
* Deprecate the `Symfony\Component\Serializer\Normalizer\ObjectNormalizer` and
10+
`Symfony\Component\Serializer\Normalizer\PropertyNormalizer` autowiring aliases, type-hint against
11+
`Symfony\Component\Serializer\Normalizer\NormalizerInterface` or implement `NormalizerAwareInterface` instead
1112

1213
6.1
1314
---

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

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -226,20 +226,37 @@ protected function renderView(string $view, array $parameters = []): string
226226
throw new \LogicException('You cannot use the "renderView" method if the Twig Bundle is not available. Try running "composer require symfony/twig-bundle".');
227227
}
228228

229+
foreach ($parameters as $k => $v) {
230+
if ($v instanceof FormInterface) {
231+
$parameters[$k] = $v->createView();
232+
}
233+
}
234+
229235
return $this->container->get('twig')->render($view, $parameters);
230236
}
231237

232238
/**
233239
* Renders a view.
240+
*
241+
* If an invalid form is found in the list of parameters, a 422 status code is returned.
234242
*/
235243
protected function render(string $view, array $parameters = [], Response $response = null): Response
236244
{
237-
$content = $this->renderView($view, $parameters);
238-
239245
if (null === $response) {
240246
$response = new Response();
241247
}
242248

249+
if (200 === $response->getStatusCode()) {
250+
foreach ($parameters as $v) {
251+
if ($v instanceof FormInterface && $v->isSubmitted() && !$v->isValid()) {
252+
$response->setStatusCode(422);
253+
break;
254+
}
255+
}
256+
}
257+
258+
$content = $this->renderView($view, $parameters);
259+
243260
$response->setContent($content);
244261

245262
return $response;
@@ -249,28 +266,12 @@ protected function render(string $view, array $parameters = [], Response $respon
249266
* Renders a view and sets the appropriate status code when a form is listed in parameters.
250267
*
251268
* If an invalid form is found in the list of parameters, a 422 status code is returned.
269+
*
270+
* @deprecated since Symfony 6.2, use render() instead
252271
*/
253272
protected function renderForm(string $view, array $parameters = [], Response $response = null): Response
254273
{
255-
if (null === $response) {
256-
$response = new Response();
257-
}
258-
259-
foreach ($parameters as $k => $v) {
260-
if ($v instanceof FormView) {
261-
throw new \LogicException(sprintf('Passing a FormView to "%s::renderForm()" is not supported, pass directly the form instead for parameter "%s".', get_debug_type($this), $k));
262-
}
263-
264-
if (!$v instanceof FormInterface) {
265-
continue;
266-
}
267-
268-
$parameters[$k] = $v->createView();
269-
270-
if (200 === $response->getStatusCode() && $v->isSubmitted() && !$v->isValid()) {
271-
$response->setStatusCode(422);
272-
}
273-
}
274+
trigger_deprecation('symfony/framework-bundle', '6.2', 'The "%s::renderForm()" method is deprecated, use "render()" instead.', get_debug_type($this));
274275

275276
return $this->render($view, $parameters, $response);
276277
}

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

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,55 @@ public function testRenderTwig()
392392
$this->assertEquals('bar', $controller->render('foo')->getContent());
393393
}
394394

395-
public function testRenderFormNew()
395+
public function testRenderViewWithForm()
396+
{
397+
$formView = new FormView();
398+
399+
$form = $this->getMockBuilder(FormInterface::class)->getMock();
400+
$form->expects($this->once())->method('createView')->willReturn($formView);
401+
402+
$twig = $this->getMockBuilder(Environment::class)->disableOriginalConstructor()->getMock();
403+
$twig->expects($this->once())->method('render')->with('foo', ['bar' => $formView])->willReturn('bar');
404+
405+
$container = new Container();
406+
$container->set('twig', $twig);
407+
408+
$controller = $this->createController();
409+
$controller->setContainer($container);
410+
411+
$content = $controller->renderView('foo', ['bar' => $form]);
412+
413+
$this->assertSame('bar', $content);
414+
}
415+
416+
public function testRenderWithFormSubmittedAndInvalid()
417+
{
418+
$formView = new FormView();
419+
420+
$form = $this->getMockBuilder(FormInterface::class)->getMock();
421+
$form->expects($this->once())->method('createView')->willReturn($formView);
422+
$form->expects($this->once())->method('isSubmitted')->willReturn(true);
423+
$form->expects($this->once())->method('isValid')->willReturn(false);
424+
425+
$twig = $this->getMockBuilder(Environment::class)->disableOriginalConstructor()->getMock();
426+
$twig->expects($this->once())->method('render')->with('foo', ['bar' => $formView])->willReturn('bar');
427+
428+
$container = new Container();
429+
$container->set('twig', $twig);
430+
431+
$controller = $this->createController();
432+
$controller->setContainer($container);
433+
434+
$response = $controller->render('foo', ['bar' => $form]);
435+
436+
$this->assertSame(422, $response->getStatusCode());
437+
$this->assertSame('bar', $response->getContent());
438+
}
439+
440+
/**
441+
* @group legacy
442+
*/
443+
public function testRenderForm()
396444
{
397445
$formView = new FormView();
398446

@@ -414,6 +462,9 @@ public function testRenderFormNew()
414462
$this->assertSame('bar', $response->getContent());
415463
}
416464

465+
/**
466+
* @group legacy
467+
*/
417468
public function testRenderFormSubmittedAndInvalid()
418469
{
419470
$formView = new FormView();

0 commit comments

Comments
 (0)