Skip to content

[Form] Throw exception when render a field which was already rendered #31704

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
May 29, 2019
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 src/Symfony/Component/Form/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CHANGELOG
* removed the `ChoiceLoaderInterface` implementation in `CountryType`, `LanguageType`, `LocaleType` and `CurrencyType`
* removed `getExtendedType()` method of the `FormTypeExtensionInterface`
* added static `getExtendedTypes()` method to the `FormTypeExtensionInterface`
* calling to `FormRenderer::searchAndRenderBlock()` method for fields which were already rendered throw a `BadMethodCallException`

4.3.0
-----
Expand Down
4 changes: 1 addition & 3 deletions src/Symfony/Component/Form/FormRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,7 @@ public function searchAndRenderBlock(FormView $view, $blockNameSuffix, array $va

if ($renderOnlyOnce && $view->isRendered()) {
// This is not allowed, because it would result in rendering same IDs multiple times, which is not valid.
@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);
// 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']));
return '';
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']));
}

// The cache key for storing the variables and types
Expand Down
17 changes: 17 additions & 0 deletions src/Symfony/Component/Form/Tests/FormRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace Symfony\Component\Form\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\FormRenderer;
use Symfony\Component\Form\FormView;

class FormRendererTest extends TestCase
{
Expand All @@ -26,4 +28,19 @@ public function testHumanize()
$this->assertEquals('Is active', $renderer->humanize('is_active'));
$this->assertEquals('Is active', $renderer->humanize('isActive'));
}

/**
* @expectedException \Symfony\Component\Form\Exception\BadMethodCallException
* @expectedExceptionMessage Field "foo" has already been rendered, save the result of previous render call to a variable and output that instead.
*/
public function testRenderARenderedField()
{
$formView = new FormView();
$formView->vars['name'] = 'foo';
$formView->setRendered();

$engine = $this->getMockBuilder('Symfony\Component\Form\FormRendererEngineInterface')->getMock();
$renderer = new FormRenderer($engine);
$renderer->searchAndRenderBlock($formView, 'row');
}
}