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

Skip to content

[FrameworkBundle] Make AbstractController::render() able to deal with forms and deprecate renderForm() #46854

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions UPGRADE-6.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ FrameworkBundle
* Deprecate the `Symfony\Component\Serializer\Normalizer\ObjectNormalizer` and
`Symfony\Component\Serializer\Normalizer\PropertyNormalizer` autowiring aliases, type-hint against
`Symfony\Component\Serializer\Normalizer\NormalizerInterface` or implement `NormalizerAwareInterface` instead
* Deprecate `AbstractController::renderForm()`, use `render()` instead

Mailer
--------
Expand Down
7 changes: 4 additions & 3 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ CHANGELOG
6.2
---

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

6.1
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
use Symfony\Component\HttpFoundation\JsonResponse;
Expand Down Expand Up @@ -219,18 +218,29 @@ protected function denyAccessUnlessGranted(mixed $attribute, mixed $subject = nu

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

foreach ($parameters as $k => $v) {
if ($v instanceof FormInterface) {
$parameters[$k] = $v->createView();
}
}

return $this->container->get('twig')->render($view, $parameters);
}

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

if (200 === $response->getStatusCode()) {
foreach ($parameters as $v) {
if ($v instanceof FormInterface && $v->isSubmitted() && !$v->isValid()) {
$response->setStatusCode(422);
break;
}
}
}

$response->setContent($content);

return $response;
Expand All @@ -249,28 +268,12 @@ protected function render(string $view, array $parameters = [], Response $respon
* Renders a view and sets the appropriate status code when a form is listed in parameters.
*
* If an invalid form is found in the list of parameters, a 422 status code is returned.
*
* @deprecated since Symfony 6.2, use render() instead
*/
protected function renderForm(string $view, array $parameters = [], Response $response = null): Response
{
if (null === $response) {
$response = new Response();
}

foreach ($parameters as $k => $v) {
if ($v instanceof FormView) {
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));
}

if (!$v instanceof FormInterface) {
continue;
}

$parameters[$k] = $v->createView();

if (200 === $response->getStatusCode() && $v->isSubmitted() && !$v->isValid()) {
$response->setStatusCode(422);
}
}
trigger_deprecation('symfony/framework-bundle', '6.2', 'The "%s::renderForm()" method is deprecated, use "render()" instead.', get_debug_type($this));

return $this->render($view, $parameters, $response);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,55 @@ public function testRenderTwig()
$this->assertEquals('bar', $controller->render('foo')->getContent());
}

public function testRenderFormNew()
public function testRenderViewWithForm()
{
$formView = new FormView();

$form = $this->getMockBuilder(FormInterface::class)->getMock();
$form->expects($this->once())->method('createView')->willReturn($formView);

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

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

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

$content = $controller->renderView('foo', ['bar' => $form]);

$this->assertSame('bar', $content);
}

public function testRenderWithFormSubmittedAndInvalid()
{
$formView = new FormView();

$form = $this->getMockBuilder(FormInterface::class)->getMock();
$form->expects($this->once())->method('createView')->willReturn($formView);
$form->expects($this->once())->method('isSubmitted')->willReturn(true);
$form->expects($this->once())->method('isValid')->willReturn(false);

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

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

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

$response = $controller->render('foo', ['bar' => $form]);

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

/**
* @group legacy
*/
public function testRenderForm()
{
$formView = new FormView();

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

/**
* @group legacy
*/
public function testRenderFormSubmittedAndInvalid()
{
$formView = new FormView();
Expand Down