Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd47f52a

Browse files
[FrameworkBundle] MakeAbstractController::render() able to deal with forms and deprecaterenderForm()
1 parent473b5b2 commitd47f52a

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
useSymfony\Component\Form\FormBuilderInterface;
2020
useSymfony\Component\Form\FormFactoryInterface;
2121
useSymfony\Component\Form\FormInterface;
22-
useSymfony\Component\Form\FormView;
2322
useSymfony\Component\HttpFoundation\BinaryFileResponse;
2423
useSymfony\Component\HttpFoundation\Exception\SessionNotFoundException;
2524
useSymfony\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
protectedfunctionrenderView(string$view,array$parameters = []):string
224225
{
225226
if (!$this->container->has('twig')) {
226227
thrownew \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 ($parametersas$k =>$v) {
231+
if ($vinstanceof 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
protectedfunctionrender(string$view,array$parameters = [],Response$response =null):Response
236246
{
@@ -240,6 +250,15 @@ protected function render(string $view, array $parameters = [], Response $respon
240250
$response =newResponse();
241251
}
242252

253+
if (200 ===$response->getStatusCode()) {
254+
foreach ($parametersas$v) {
255+
if ($vinstanceof 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
protectedfunctionrenderForm(string$view,array$parameters = [],Response$response =null):Response
254275
{
255-
if (null ===$response) {
256-
$response =newResponse();
257-
}
258-
259-
foreach ($parametersas$k =>$v) {
260-
if ($vinstanceof FormView) {
261-
thrownew \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 (!$vinstanceof 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-
publicfunctiontestRenderFormNew()
395+
publicfunctiontestRenderViewWithForm()
396+
{
397+
$formView =newFormView();
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 =newContainer();
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+
publicfunctiontestRenderWithFormSubmittedAndInvalid()
417+
{
418+
$formView =newFormView();
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 =newContainer();
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+
publicfunctiontestRenderForm()
396444
{
397445
$formView =newFormView();
398446

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

465+
/**
466+
* @group legacy
467+
*/
417468
publicfunctiontestRenderFormSubmittedAndInvalid()
418469
{
419470
$formView =newFormView();

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp