Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d47f52a

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

File tree

4 files changed

+80
-24
lines changed

4 files changed

+80
-24
lines changed

UPGRADE-6.2.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ FrameworkBundle
77
* Deprecate the `Symfony\Component\Serializer\Normalizer\ObjectNormalizer` and
88
`Symfony\Component\Serializer\Normalizer\PropertyNormalizer` autowiring aliases, type-hint against
99
`Symfony\Component\Serializer\Normalizer\NormalizerInterface` or implement `NormalizerAwareInterface` instead
10+
* Deprecate `AbstractController::renderForm()`, use `render()` instead
1011

1112
Mailer
1213
--------

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: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use Symfony\Component\Form\FormBuilderInterface;
2020
use Symfony\Component\Form\FormFactoryInterface;
2121
use Symfony\Component\Form\FormInterface;
22-
use Symfony\Component\Form\FormView;
2322
use Symfony\Component\HttpFoundation\BinaryFileResponse;
2423
use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
2524
use Symfony\Component\HttpFoundation\JsonResponse;
@@ -219,18 +218,29 @@ protected function denyAccessUnlessGranted(mixed $attribute, mixed $subject = nu
219218

220219
/**
221220
* Returns a rendered view.
221+
*
222+
* Forms found in parameters are auto-cast to form views.
222223
*/
223224
protected function renderView(string $view, array $parameters = []): string
224225
{
225226
if (!$this->container->has('twig')) {
226227
throw new \LogicException('You cannot use the "renderView" method if the Twig Bundle is not available. Try running "composer require symfony/twig-bundle".');
227228
}
228229

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

232239
/**
233240
* Renders a view.
241+
*
242+
* If an invalid form is found in the list of parameters, a 422 status code is returned.
243+
* Forms found in parameters are auto-cast to form views.
234244
*/
235245
protected function render(string $view, array $parameters = [], Response $response = null): Response
236246
{
@@ -240,6 +250,15 @@ protected function render(string $view, array $parameters = [], Response $respon
240250
$response = new Response();
241251
}
242252

253+
if (200 === $response->getStatusCode()) {
254+
foreach ($parameters as $v) {
255+
if ($v instanceof FormInterface && $v->isSubmitted() && !$v->isValid()) {
256+
$response->setStatusCode(422);
257+
break;
258+
}
259+
}
260+
}
261+
243262
$response->setContent($content);
244263

245264
return $response;
@@ -249,28 +268,12 @@ protected function render(string $view, array $parameters = [], Response $respon
249268
* Renders a view and sets the appropriate status code when a form is listed in parameters.
250269
*
251270
* If an invalid form is found in the list of parameters, a 422 status code is returned.
271+
*
272+
* @deprecated since Symfony 6.2, use render() instead
252273
*/
253274
protected function renderForm(string $view, array $parameters = [], Response $response = null): Response
254275
{
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-
}
276+
trigger_deprecation('symfony/framework-bundle', '6.2', 'The "%s::renderForm()" method is deprecated, use "render()" instead.', get_debug_type($this));
274277

275278
return $this->render($view, $parameters, $response);
276279
}

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)