[Form] Fixed caching of block names when types of forms with the same unique block ID differ

This commit is contained in:
Bernhard Schussek 2012-07-25 13:42:52 +02:00
parent 2bc358dbcd
commit 6b17640647
4 changed files with 59 additions and 1 deletions

View File

@ -21,7 +21,7 @@ abstract class AbstractRendererEngine implements FormRendererEngineInterface
/**
* The variable in {@link FormView} used as cache key.
*/
const CACHE_KEY_VAR = 'full_block_name';
const CACHE_KEY_VAR = 'cache_key';
/**
* @var array

View File

@ -129,6 +129,13 @@ class FormType extends AbstractType
'compound' => $form->getConfig()->getCompound(),
'types' => $types,
'translation_domain' => $translationDomain,
// Using the block name here speeds up performance in collection
// forms, where each entry has the same full block name.
// Including the type is important too, because if rows of a
// collection form have different types (dynamically), they should
// be rendered differently.
// https://github.com/symfony/symfony/issues/5038
'cache_key' => $fullBlockName . '_' . $form->getConfig()->getType()->getName(),
));
}

View File

@ -12,6 +12,7 @@
namespace Symfony\Component\Form\Tests;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\Tests\Fixtures\AlternatingRowType;
abstract class AbstractDivLayoutTest extends AbstractLayoutTest
{
@ -283,6 +284,29 @@ abstract class AbstractDivLayoutTest extends AbstractLayoutTest
);
}
// https://github.com/symfony/symfony/issues/5038
public function testCollectionWithAlternatingRowTypes()
{
$data = array(
array('title' => 'a'),
array('title' => 'b'),
);
$form = $this->factory->createNamed('name', 'collection', $data, array(
'type' => new AlternatingRowType(),
));
$this->assertWidgetMatchesXpath($form->createView(), array(),
'/div
[
./div[./div/div/input[@type="text"][@value="a"]]
/following-sibling::div[./div/div/textarea[.="b"]]
]
[count(./div[./div/div/input])=1]
[count(./div[./div/div/textarea])=1]
'
);
}
public function testEmptyCollection()
{
$form = $this->factory->createNamed('name', 'collection', array(), array(

View File

@ -0,0 +1,27 @@
<?php
namespace Symfony\Component\Form\Tests\Fixtures;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormBuilderInterface;
class AlternatingRowType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$formFactory = $builder->getFormFactory();
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($formFactory) {
$form = $event->getForm();
$type = $form->getName() % 2 === 0 ? 'text' : 'textarea';
$form->add($formFactory->createNamed('title', $type));
});
}
public function getName()
{
return 'alternating_row';
}
}