Skip to content

[Form] set unique block prefix with any namespace #17874

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

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion src/Symfony/Component/Form/AbstractRendererEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ private function loadResourceForBlockNameHierarchy($cacheKey, FormView $view, ar
// The next two if statements contain slightly duplicated code. This is by intention
// and tries to avoid execution of unnecessary checks in order to increase performance.

if (isset($this->resources[$cacheKey][$parentBlockName])) {
if (isset($this->resources[$cacheKey][$parentBlockName]) && $this->resources[$cacheKey][$parentBlockName] !== false) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is missing a corresponding test.

Also you are reducing the impact of a performance optimization here. Could you benchmark the result for big forms?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i can't find any test for the Symfony\Component\Form\AbstractRendererEngine or Symfony\Component\Form\Extension\Templating\TemplatingRendererEngine, could you show where are they located?

could you show the current benchmark?

Copy link
Contributor

@issei-m issei-m May 12, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be the yoda style like:

false !== $this->resources[$cacheKey][$parentBlockName]

// It may happen that the parent block is already loaded, but its level is not.
// In this case, the parent block must have been loaded by loadResourceForBlock(),
// which does not check the hierarchy of the block. Subsequently the block must have
Expand Down
5 changes: 4 additions & 1 deletion src/Symfony/Component/Form/Extension/Core/Type/BaseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ public function buildView(FormView $view, FormInterface $form, array $options)

$blockPrefixes = array();
for ($type = $form->getConfig()->getType(); null !== $type; $type = $type->getParent()) {
array_unshift($blockPrefixes, $type->getBlockPrefix());
$blockPrefix = $type->getBlockPrefix();
if (!in_array($blockPrefix, $blockPrefixes)) {
array_unshift($blockPrefixes, $blockPrefix);
}
}
$blockPrefixes[] = $uniqueBlockPrefix;

Expand Down
40 changes: 40 additions & 0 deletions src/Symfony/Component/Form/Tests/FixtureTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Form\Tests;

class FixtureTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
{
/**
* @dataProvider provideTypeClassBlockPrefix
*/
public function testUniqueBlockPrefixes($typeClass, $blockPrefixes)
{
$form = $this->factory->create($typeClass);
$view = $form->createView();
$this->assertEquals($view->vars['block_prefixes'], $blockPrefixes);
}

/**
* @return array
*/
public function provideTypeClassBlockPrefix()
{
return array(
array(__NAMESPACE__.'\Fixtures\Foo', ['form','foo','_foo']),
array(__NAMESPACE__.'\Fixtures\Type', ['form', 'type', '_type']),
array(__NAMESPACE__.'\Fixtures\FooBarHTMLType',['form', 'foo_bar_html', '_foo_bar_html']),
array(__NAMESPACE__.'\Fixtures\Foo1Bar2Type', ['form', 'foo1_bar2', '_foo1_bar2']),
array(__NAMESPACE__.'\Fixtures\OtherType\Foo1Bar2Type', ['form', 'foo1_bar2', '_foo1_bar2']),
array(__NAMESPACE__.'\Fixtures\FBooType', ['form', 'f_boo', '_f_boo']),
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Form\Tests\Fixtures\OtherType;

use Symfony\Component\Form\AbstractType;

class Foo1Bar2Type extends AbstractType
{
public function getParent()
{
return 'Symfony\Component\Form\Tests\Fixtures\Foo1Bar2Type';
}
}