Skip to content

Commit

Permalink
minor #31704 [Form] Throw exception when render a field which was alr…
Browse files Browse the repository at this point in the history
…eady rendered (yceruto)

This PR was merged into the 5.0-dev branch.

Discussion
----------

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

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

See #27247

Commits
-------

d1bbad0 Throw exception when render a field which was already rendered
  • Loading branch information
nicolas-grekas committed May 29, 2019
2 parents bb9cf60 + d1bbad0 commit 8401f27
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
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
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
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');
}
}

0 comments on commit 8401f27

Please sign in to comment.