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

Commit7852349

Browse files
committed
[FrameworkBundle] Introduce AbstractController::renderForm()
1 parent094b507 commit7852349

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+
protectedfunctionrenderForm(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
publicfunctionhandleForm(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
useSymfony\Component\Form\Form;
2222
useSymfony\Component\Form\FormBuilderInterface;
2323
useSymfony\Component\Form\FormConfigInterface;
24+
useSymfony\Component\Form\FormError;
2425
useSymfony\Component\Form\FormFactoryInterface;
2526
useSymfony\Component\Form\FormInterface;
2627
useSymfony\Component\HttpFoundation\BinaryFileResponse;
@@ -411,6 +412,44 @@ public function testRenderTwig()
411412
$this->assertEquals('bar',$controller->render('foo')->getContent());
412413
}
413414

415+
publicfunctiontestRenderFormNew()
416+
{
417+
$twig =$this->createMock(Environment::class);
418+
$twig->expects($this->once())->method('render')->willReturn('bar');
419+
420+
$container =newContainer();
421+
$container->set('twig',$twig);
422+
423+
$controller =$this->createController();
424+
$controller->setContainer($container);
425+
$form =newForm($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+
publicfunctiontestRenderFormSubmittedAndInvalid()
433+
{
434+
$twig =$this->createMock(Environment::class);
435+
$twig->expects($this->once())->method('render')->willReturn('bar');
436+
437+
$container =newContainer();
438+
$container->set('twig',$twig);
439+
440+
$controller =$this->createController();
441+
$controller->setContainer($container);
442+
$form =newForm($this->createMock(FormConfigInterface::class));
443+
$formReflection =new \ReflectionProperty($form,'submitted');
444+
$formReflection->setAccessible(true);
445+
$formReflection->setValue($form,true);
446+
$form->addError(newFormError('message'));
447+
448+
$response =$controller->renderForm('foo',$form);
449+
$this->assertEquals(422,$response->getStatusCode());
450+
$this->assertEquals('bar',$response->getContent());
451+
}
452+
414453
publicfunctiontestStreamTwig()
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
publicfunctiontestHandleFormNotSubmitted()
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
publicfunctiontestHandleFormInvalid()
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
publicfunctiontestHandleFormValid()
471519
{
472520
$form =$this->createMock(FormInterface::class);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp