8000 minor #41190 [FrameworkBundle] improve AbstractController::renderForm… · symfony/symfony@2ac23c6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2ac23c6

Browse files
minor #41190 [FrameworkBundle] improve AbstractController::renderForm() (nicolas-grekas)
This PR was merged into the 5.3-dev branch. Discussion ---------- [FrameworkBundle] improve AbstractController::renderForm() | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - Even better than #41178, this requires a simple change on apps, and is compatible with multiple forms. Usage: ```diff - return $this->render('thing/new.html.twig', [ + return $this->renderForm('thing/new.html.twig', [ 'thing' => $thing, - 'form' => $form->createView(), + 'form' => $form, ]); ``` In 5.4, we could even deprecate passing a FormView to render() so that we can always set the 422. Commits ------- e244d31 [FrameworkBundle] improve AbstractController::renderForm()
2 parents af77943 + e244d31 commit 2ac23c6

File tree

3 files changed

+31
-18
lines changed

3 files changed

+31
-18
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ CHANGELOG
77
* Deprecate the `session.storage` alias and `session.storage.*` services, use the `session.storage.factory` alias and `session.storage.factory.*` services instead
88
* Deprecate the `framework.session.storage_id` configuration option, use the `framework.session.storage_factory_id` configuration option instead
99
* Deprecate the `session` service and the `SessionInterface` alias, use the `Request::getSession()` or the new `RequestStack::getSession()` methods instead
10-
* Added `AbstractController::renderForm()` to render a form and set the appropriate HTTP status code
11-
* Added support for configuring PHP error level to log levels
12-
* Added the `dispatcher` option to `debug:event-dispatcher`
13-
* Added the `event_dispatcher.dispatcher` tag
14-
* Added `assertResponseFormatSame()` in `BrowserKitAssertionsTrait`
10+
* Add `AbstractController::renderForm()` to render a form and set the appropriate HTTP status code
11+
* Add support for configuring PHP error level to log levels
12+
* Add the `dispatcher` option to `debug:event-dispatcher`
13+
* Add the `event_dispatcher.dispatcher` tag
14+
* Add `assertResponseFormatSame()` in `BrowserKitAssertionsTrait`
1515
* Add support for configuring UUID factory services
1616
* Add tag `assets.package` to register asset packages
1717
* Add support to use a PSR-6 compatible cache for Doctrine annotations

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

Lines changed: 22 additions & 9 deletions
8000
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\Form\FormBuilderInterface;
2121
use Symfony\Component\Form\FormFactoryInterface;
2222
use Symfony\Component\Form\FormInterface;
23+
use Symfony\Component\Form\FormView;
2324
use Symfony\Component\HttpFoundation\BinaryFileResponse;
2425
use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
2526
use Symfony\Component\HttpFoundation\JsonResponse;
@@ -267,21 +268,33 @@ protected function render(string $view, array $parameters = [], Response $respon
267268
}
268269

269270
/**
270-
* Renders a view for a form.
271+
* Renders a view and sets the appropriate status code when a form is listed in parameters.
271272
*
272-
* The FormView instance is passed to the template in a variable named
273-
* "form" (can be changed via $formVar argument).
274-
* If the form is invalid, a 422 status code is returned.
273+
* If an invalid form is found in the list of parameters, a 422 status code is returned.
275274
*/
276-
protected function renderForm(string $view, FormInterface $form, array $parameters = [], Response $response = null, string $formVar = 'form'): Response
275+
protected function renderForm(string $view, array $parameters = [], Response $response = null): Response
277276
{
278-
$response = $this->render($view, [$formVar => $form->createView()] + $parameters, $response);
277+
if (null === $response) {
278+
$response = new Response();
279+
}
280+
281+
foreach ($parameters as $k => $v) {
282+
if ($v instanceof FormView) {
283+
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));
284+
}
279285

280-
if ($form->isSubmitted() && !$form->isValid()) {
281-
$response->setStatusCode(422);
286+
if (!$v instanceof FormInterface) {
287+
continue;
288+
}
289+
290+
$parameters[$k] = $v->createView();
291+
292+
if (200 === $response->getStatusCode() && $v->isSubmitted() && !$v->isValid()) {
293+
$response->setStatusCode(422);
294+
}
282295
}
283296

284-
return $response;
297+
return $this->render($view, $parameters, $response);
285298
}
286299

287300
/**

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -420,15 +420,15 @@ public function testRenderFormNew()
420420
$form->expects($this->once())->method('createView')->willReturn($formView);
421421

422422
$twig = $this->getMockBuilder(Environment::class)->disableOriginalConstructor()->getMock();
423-
$twig->expects($this->once())->method('render')->with('foo', ['form' => $formView, 'bar' => 'bar'])->willReturn('bar');
423+
$twig->expects($this->once())->method('render')->with('foo', ['bar' => $formView])->willReturn('bar');
424424

425425
$container = new Container();
426426
$container->set('twig', $twig);
427427

428428
$controller = $this->createController();
429429
$controller->setContainer($container);
430430

431-
$response = $controller->renderForm('foo', $form, ['bar' => 'bar']);
431+
$response = $controller->renderForm('foo', ['bar' => $form]);
432432

433433
$this->assertTrue($response->isSuccessful());
434434
$this->assertSame('bar', $response->getContent());
@@ -444,15 +444,15 @@ public function testRenderFormSubmittedAndInvalid()
444444
$form->expects($this->once())->method('isValid')->willReturn(false);
445445

446446
$twig = $this->getMockBuilder(Environment::class)->disableOriginalConstructor()->getMock();
447-
$twig->expects($this->once())->method('render')->with('foo', ['myForm' => $formView, 'bar' => 'bar'])->willReturn('bar');
447+
$twig->expects($this->once())->method('render')->with('foo', ['bar' => $formView])->willReturn('bar');
448448

449449
$container = new Container();
450450
$container->set('twig', $twig);
451451

452452
$controller = $this->createController();
453453
$controller->setContainer($container);
454454

455-
$response = $controller->renderForm('foo', $form, ['bar' => 'bar'], null, 'myForm');
455+
$response = $controller->renderForm('foo', ['bar' => $form]);
456456

457457
$this->assertSame(422, $response->getStatusCode());
458458
$this->assertSame('bar', $response->getContent());

0 commit comments

Comments
 (0)
0