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

Skip to content

Commit 51d1ed5

Browse files
committed
Throw exception when render a field which was already rendered
1 parent 2bf74ce commit 51d1ed5

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

src/Symfony/Component/Form/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
-----
66

77
* removed the `ChoiceLoaderInterface` implementation in `CountryType`, `LanguageType`, `LocaleType` and `CurrencyType`
8+
* calling to `FormRenderer::searchAndRenderBlock()` method for fields which were already rendered throw a `BadMethodCallException`
89

910
4.3.0
1011
-----

src/Symfony/Component/Form/FormRenderer.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,7 @@ public function searchAndRenderBlock(FormView $view, $blockNameSuffix, array $va
133133

134134
if ($renderOnlyOnce && $view->isRendered()) {
135135
// This is not allowed, because it would result in rendering same IDs multiple times, which is not valid.
136-
@trigger_error(sprintf('You are calling "form_%s" for field "%s" which has already been rendered before, trying to render fields which were already rendered is deprecated since Symfony 4.2 and will throw an exception in 5.0.', $blockNameSuffix, $view->vars['name']), E_USER_DEPRECATED);
137-
// throw new BadMethodCallException(sprintf('Field "%s" has already been rendered. Save result of previous render call to variable and output that instead.', $view->vars['name']));
138-
return '';
136+
throw new BadMethodCallException(sprintf('Field "%s" has already been rendered, save the result of previous render call to a variable and output that instead.', $view->vars['name']));
139137
}
140138

141139
// The cache key for storing the variables and types

src/Symfony/Component/Form/Tests/FormRendererTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Component\Form\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Form\FormRenderer;
16+
use Symfony\Component\Form\FormView;
1517

1618
class FormRendererTest extends TestCase
1719
{
@@ -26,4 +28,19 @@ public function testHumanize()
2628
$this->assertEquals('Is active', $renderer->humanize('is_active'));
2729
$this->assertEquals('Is active', $renderer->humanize('isActive'));
2830
}
31+
32+
/**
33+
* @expectedException \Symfony\Component\Form\Exception\BadMethodCallException
34+
* @expectedExceptionMessage Field "foo" has already been rendered, save the result of previous render call to a variable and output that instead.
35+
*/
36+
public function testRenderARenderedField()
37+
{
38+
$formView = new FormView();
39+
$formView->vars['name'] = 'foo';
40+
$formView->setRendered();
41+
42+
$engine = $this->getMockBuilder('Symfony\Component\Form\FormRendererEngineInterface')->getMock();
43+
$renderer = new FormRenderer($engine);
44+
$renderer->searchAndRenderBlock($formView, 'row');
45+
}
2946
}

0 commit comments

Comments
 (0)