Rename CollectionType options for entries

This commit is contained in:
WouterJ 2015-03-01 23:32:42 +01:00
parent c8475c938d
commit 942a2370c6
4 changed files with 81 additions and 34 deletions

View File

@ -27,17 +27,17 @@ class CollectionType extends AbstractType
public function buildForm(FormBuilderInterface $builder, array $options) public function buildForm(FormBuilderInterface $builder, array $options)
{ {
if ($options['allow_add'] && $options['prototype']) { if ($options['allow_add'] && $options['prototype']) {
$prototype = $builder->create($options['prototype_name'], $options['type'], array_replace(array( $prototype = $builder->create($options['prototype_name'], $options['entry_type'], array_replace(array(
'label' => $options['prototype_name'].'label__', 'label' => $options['prototype_name'].'label__',
), $options['options'], array( ), $options['entry_options'], array(
'data' => $options['prototype_data'], 'data' => $options['prototype_data'],
))); )));
$builder->setAttribute('prototype', $prototype->getForm()); $builder->setAttribute('prototype', $prototype->getForm());
} }
$resizeListener = new ResizeFormListener( $resizeListener = new ResizeFormListener(
$options['type'], $options['entry_type'],
$options['options'], $options['entry_options'],
$options['allow_add'], $options['allow_add'],
$options['allow_delete'], $options['allow_delete'],
$options['delete_empty'] $options['delete_empty']
@ -76,11 +76,37 @@ class CollectionType extends AbstractType
*/ */
public function configureOptions(OptionsResolver $resolver) public function configureOptions(OptionsResolver $resolver)
{ {
$optionsNormalizer = function (Options $options, $value) { $entryOptionsNormalizer = function (Options $options, $value) {
$value['block_name'] = 'entry'; $value['block_name'] = 'entry';
return $value; return $value;
}; };
$optionsNormalizer = function (Options $options, $value) use ($entryOptionsNormalizer) {
if (null !== $value) {
@trigger_error('The form option "options" is deprecated since version 2.8 and will be removed in 3.0. Use "entry_options" instead.', E_USER_DEPRECATED);
}
return $entryOptionsNormalizer($options, $value);
};
$typeNormalizer = function (Options $options, $value) {
if (null !== $value) {
@trigger_error('The form option "type" is deprecated since version 2.8 and will be removed in 3.0. Use "entry_type" instead.', E_USER_DEPRECATED);
}
};
$entryType = function (Options $options) {
if (null !== $options['type']) {
return $options['type'];
}
return __NAMESPACE__.'\TextType';
};
$entryOptions = function (Options $options) {
if (1 === count($options['options']) && isset($options['block_name'])) {
return array();
}
return $options['options'];
};
$resolver->setDefaults(array( $resolver->setDefaults(array(
'allow_add' => false, 'allow_add' => false,
@ -88,12 +114,17 @@ class CollectionType extends AbstractType
'prototype' => true, 'prototype' => true,
'prototype_data' => null, 'prototype_data' => null,
'prototype_name' => '__name__', 'prototype_name' => '__name__',
'type' => __NAMESPACE__.'\TextType', // deprecated as of Symfony 2.8, to be removed in Symfony 3.0. Use entry_type instead
'options' => array(), 'type' => null,
// deprecated as of Symfony 2.8, to be removed in Symfony 3.0. Use entry_options instead
'options' => null,
'entry_type' => $entryType,
'entry_options' => $entryOptions,
'delete_empty' => false, 'delete_empty' => false,
)); ));
$resolver->setNormalizer('options', $optionsNormalizer); $resolver->setNormalizer('options', $optionsNormalizer);
$resolver->setNormalizer('entry_options', $entryOptionsNormalizer);
} }
/** /**

View File

@ -284,7 +284,7 @@ abstract class AbstractDivLayoutTest extends AbstractLayoutTest
public function testCollection() public function testCollection()
{ {
$form = $this->factory->createNamed('names', 'Symfony\Component\Form\Extension\Core\Type\CollectionType', array('a', 'b'), array( $form = $this->factory->createNamed('names', 'Symfony\Component\Form\Extension\Core\Type\CollectionType', array('a', 'b'), array(
'type' => 'Symfony\Component\Form\Extension\Core\Type\TextType', 'entry_type' => 'Symfony\Component\Form\Extension\Core\Type\TextType',
)); ));
$this->assertWidgetMatchesXpath($form->createView(), array(), $this->assertWidgetMatchesXpath($form->createView(), array(),
@ -306,7 +306,7 @@ abstract class AbstractDivLayoutTest extends AbstractLayoutTest
array('title' => 'b'), array('title' => 'b'),
); );
$form = $this->factory->createNamed('names', 'Symfony\Component\Form\Extension\Core\Type\CollectionType', $data, array( $form = $this->factory->createNamed('names', 'Symfony\Component\Form\Extension\Core\Type\CollectionType', $data, array(
'type' => 'Symfony\Component\Form\Tests\Fixtures\AlternatingRowType', 'entry_type' => 'Symfony\Component\Form\Tests\Fixtures\AlternatingRowType',
)); ));
$this->assertWidgetMatchesXpath($form->createView(), array(), $this->assertWidgetMatchesXpath($form->createView(), array(),
@ -324,7 +324,7 @@ abstract class AbstractDivLayoutTest extends AbstractLayoutTest
public function testEmptyCollection() public function testEmptyCollection()
{ {
$form = $this->factory->createNamed('names', 'Symfony\Component\Form\Extension\Core\Type\CollectionType', array(), array( $form = $this->factory->createNamed('names', 'Symfony\Component\Form\Extension\Core\Type\CollectionType', array(), array(
'type' => 'Symfony\Component\Form\Extension\Core\Type\TextType', 'entry_type' => 'Symfony\Component\Form\Extension\Core\Type\TextType',
)); ));
$this->assertWidgetMatchesXpath($form->createView(), array(), $this->assertWidgetMatchesXpath($form->createView(), array(),
@ -341,7 +341,7 @@ abstract class AbstractDivLayoutTest extends AbstractLayoutTest
'collection', 'collection',
'Symfony\Component\Form\Extension\Core\Type\CollectionType', 'Symfony\Component\Form\Extension\Core\Type\CollectionType',
array('a', 'b'), array('a', 'b'),
array('type' => 'Symfony\Component\Form\Extension\Core\Type\TextType') array('entry_type' => 'Symfony\Component\Form\Extension\Core\Type\TextType')
); );
$form = $this->factory->createNamedBuilder('form', 'Symfony\Component\Form\Extension\Core\Type\FormType') $form = $this->factory->createNamedBuilder('form', 'Symfony\Component\Form\Extension\Core\Type\FormType')

View File

@ -22,7 +22,7 @@ class CollectionTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
public function testLegacyName() public function testLegacyName()
{ {
$form = $this->factory->create('collection', array( $form = $this->factory->create('collection', array(
'type' => 'text', 'entry_type' => 'text',
)); ));
$this->assertSame('collection', $form->getConfig()->getType()->getName()); $this->assertSame('collection', $form->getConfig()->getType()->getName());
@ -40,8 +40,8 @@ class CollectionTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
public function testSetDataAdjustsSize() public function testSetDataAdjustsSize()
{ {
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', null, array( $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', null, array(
'type' => 'Symfony\Component\Form\Extension\Core\Type\TextType', 'entry_type' => 'Symfony\Component\Form\Extension\Core\Type\TextType',
'options' => array( 'entry_options' => array(
'attr' => array('maxlength' => 20), 'attr' => array('maxlength' => 20),
), ),
)); ));
@ -69,7 +69,7 @@ class CollectionTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
public function testThrowsExceptionIfObjectIsNotTraversable() public function testThrowsExceptionIfObjectIsNotTraversable()
{ {
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', null, array( $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', null, array(
'type' => 'Symfony\Component\Form\Extension\Core\Type\TextType', 'entry_type' => 'Symfony\Component\Form\Extension\Core\Type\TextType',
)); ));
$this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException'); $this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
$form->setData(new \stdClass()); $form->setData(new \stdClass());
@ -78,7 +78,7 @@ class CollectionTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
public function testNotResizedIfSubmittedWithMissingData() public function testNotResizedIfSubmittedWithMissingData()
{ {
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', null, array( $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', null, array(
'type' => 'Symfony\Component\Form\Extension\Core\Type\TextType', 'entry_type' => 'Symfony\Component\Form\Extension\Core\Type\TextType',
)); ));
$form->setData(array('foo@foo.com', 'bar@bar.com')); $form->setData(array('foo@foo.com', 'bar@bar.com'));
$form->submit(array('foo@bar.com')); $form->submit(array('foo@bar.com'));
@ -92,7 +92,7 @@ class CollectionTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
public function testResizedDownIfSubmittedWithMissingDataAndAllowDelete() public function testResizedDownIfSubmittedWithMissingDataAndAllowDelete()
{ {
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', null, array( $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', null, array(
'type' => 'Symfony\Component\Form\Extension\Core\Type\TextType', 'entry_type' => 'Symfony\Component\Form\Extension\Core\Type\TextType',
'allow_delete' => true, 'allow_delete' => true,
)); ));
$form->setData(array('foo@foo.com', 'bar@bar.com')); $form->setData(array('foo@foo.com', 'bar@bar.com'));
@ -107,7 +107,7 @@ class CollectionTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
public function testResizedDownIfSubmittedWithEmptyDataAndDeleteEmpty() public function testResizedDownIfSubmittedWithEmptyDataAndDeleteEmpty()
{ {
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', null, array( $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', null, array(
'type' => 'Symfony\Component\Form\Extension\Core\Type\TextType', 'entry_type' => 'Symfony\Component\Form\Extension\Core\Type\TextType',
'allow_delete' => true, 'allow_delete' => true,
'delete_empty' => true, 'delete_empty' => true,
)); ));
@ -124,7 +124,7 @@ class CollectionTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
public function testDontAddEmptyDataIfDeleteEmpty() public function testDontAddEmptyDataIfDeleteEmpty()
{ {
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', null, array( $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', null, array(
'type' => 'Symfony\Component\Form\Extension\Core\Type\TextType', 'entry_type' => 'Symfony\Component\Form\Extension\Core\Type\TextType',
'allow_add' => true, 'allow_add' => true,
'delete_empty' => true, 'delete_empty' => true,
)); ));
@ -141,7 +141,7 @@ class CollectionTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
public function testNoDeleteEmptyIfDeleteNotAllowed() public function testNoDeleteEmptyIfDeleteNotAllowed()
{ {
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', null, array( $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', null, array(
'type' => 'Symfony\Component\Form\Extension\Core\Type\TextType', 'entry_type' => 'Symfony\Component\Form\Extension\Core\Type\TextType',
'allow_delete' => false, 'allow_delete' => false,
'delete_empty' => true, 'delete_empty' => true,
)); ));
@ -156,10 +156,10 @@ class CollectionTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
public function testResizedDownIfSubmittedWithCompoundEmptyDataAndDeleteEmpty() public function testResizedDownIfSubmittedWithCompoundEmptyDataAndDeleteEmpty()
{ {
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', null, array( $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', null, array(
'type' => 'Symfony\Component\Form\Tests\Fixtures\AuthorType', 'entry_type' => 'Symfony\Component\Form\Tests\Fixtures\AuthorType',
// If the field is not required, no new Author will be created if the // If the field is not required, no new Author will be created if the
// form is completely empty // form is completely empty
'options' => array('required' => false), 'entry_options' => array('required' => false),
'allow_add' => true, 'allow_add' => true,
'delete_empty' => true, 'delete_empty' => true,
)); ));
@ -179,7 +179,7 @@ class CollectionTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
public function testNotResizedIfSubmittedWithExtraData() public function testNotResizedIfSubmittedWithExtraData()
{ {
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', null, array( $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', null, array(
'type' => 'Symfony\Component\Form\Extension\Core\Type\TextType', 'entry_type' => 'Symfony\Component\Form\Extension\Core\Type\TextType',
)); ));
$form->setData(array('foo@bar.com')); $form->setData(array('foo@bar.com'));
$form->submit(array('foo@foo.com', 'bar@bar.com')); $form->submit(array('foo@foo.com', 'bar@bar.com'));
@ -192,7 +192,7 @@ class CollectionTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
public function testResizedUpIfSubmittedWithExtraDataAndAllowAdd() public function testResizedUpIfSubmittedWithExtraDataAndAllowAdd()
{ {
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', null, array( $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', null, array(
'type' => 'Symfony\Component\Form\Extension\Core\Type\TextType', 'entry_type' => 'Symfony\Component\Form\Extension\Core\Type\TextType',
'allow_add' => true, 'allow_add' => true,
)); ));
$form->setData(array('foo@bar.com')); $form->setData(array('foo@bar.com'));
@ -208,7 +208,7 @@ class CollectionTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
public function testAllowAddButNoPrototype() public function testAllowAddButNoPrototype()
{ {
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', null, array( $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', null, array(
'type' => 'Symfony\Component\Form\Extension\Core\Type\FormType', 'entry_type' => 'Symfony\Component\Form\Extension\Core\Type\FormType',
'allow_add' => true, 'allow_add' => true,
'prototype' => false, 'prototype' => false,
)); ));
@ -220,7 +220,7 @@ class CollectionTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
{ {
$form = $this->factory $form = $this->factory
->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', null, array( ->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', null, array(
'type' => 'Symfony\Component\Form\Extension\Core\Type\FileType', 'entry_type' => 'Symfony\Component\Form\Extension\Core\Type\FileType',
'allow_add' => true, 'allow_add' => true,
'prototype' => true, 'prototype' => true,
)) ))
@ -232,7 +232,7 @@ class CollectionTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
public function testGetDataDoesNotContainsPrototypeNameBeforeDataAreSet() public function testGetDataDoesNotContainsPrototypeNameBeforeDataAreSet()
{ {
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', array(), array( $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', array(), array(
'type' => 'Symfony\Component\Form\Extension\Core\Type\FileType', 'entry_type' => 'Symfony\Component\Form\Extension\Core\Type\FileType',
'prototype' => true, 'prototype' => true,
'allow_add' => true, 'allow_add' => true,
)); ));
@ -244,7 +244,7 @@ class CollectionTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
public function testGetDataDoesNotContainsPrototypeNameAfterDataAreSet() public function testGetDataDoesNotContainsPrototypeNameAfterDataAreSet()
{ {
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', array(), array( $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', array(), array(
'type' => 'Symfony\Component\Form\Extension\Core\Type\FileType', 'entry_type' => 'Symfony\Component\Form\Extension\Core\Type\FileType',
'allow_add' => true, 'allow_add' => true,
'prototype' => true, 'prototype' => true,
)); ));
@ -257,7 +257,7 @@ class CollectionTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
public function testPrototypeNameOption() public function testPrototypeNameOption()
{ {
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', null, array( $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', null, array(
'type' => 'Symfony\Component\Form\Extension\Core\Type\FormType', 'entry_type' => 'Symfony\Component\Form\Extension\Core\Type\FormType',
'prototype' => true, 'prototype' => true,
'allow_add' => true, 'allow_add' => true,
)); ));
@ -265,7 +265,7 @@ class CollectionTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
$this->assertSame('__name__', $form->getConfig()->getAttribute('prototype')->getName(), '__name__ is the default'); $this->assertSame('__name__', $form->getConfig()->getAttribute('prototype')->getName(), '__name__ is the default');
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', null, array( $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', null, array(
'type' => 'Symfony\Component\Form\Extension\Core\Type\FormType', 'entry_type' => 'Symfony\Component\Form\Extension\Core\Type\FormType',
'prototype' => true, 'prototype' => true,
'allow_add' => true, 'allow_add' => true,
'prototype_name' => '__test__', 'prototype_name' => '__test__',
@ -274,10 +274,26 @@ class CollectionTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
$this->assertSame('__test__', $form->getConfig()->getAttribute('prototype')->getName()); $this->assertSame('__test__', $form->getConfig()->getAttribute('prototype')->getName());
} }
/**
* @group legacy
*/
public function testLegacyEntryOptions()
{
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', array(), array(
'type' => 'Symfony\Component\Form\Extension\Core\Type\NumberType',
'options' => array('attr' => array('maxlength' => '10')),
));
$resolvedOptions = $form->getConfig()->getOptions();
$this->assertEquals('Symfony\Component\Form\Extension\Core\Type\NumberType', $resolvedOptions['entry_type']);
$this->assertEquals(array('attr' => array('maxlength' => '10'), 'block_name' => 'entry'), $resolvedOptions['entry_options']);
}
public function testPrototypeDefaultLabel() public function testPrototypeDefaultLabel()
{ {
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', array(), array( $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', array(), array(
'type' => 'Symfony\Component\Form\Extension\Core\Type\FileType', 'entry_type' => 'Symfony\Component\Form\Extension\Core\Type\FileType',
'allow_add' => true, 'allow_add' => true,
'prototype' => true, 'prototype' => true,
'prototype_name' => '__test__', 'prototype_name' => '__test__',
@ -293,7 +309,7 @@ class CollectionTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
'allow_add' => true, 'allow_add' => true,
'prototype' => true, 'prototype' => true,
'prototype_data' => 'foo', 'prototype_data' => 'foo',
'options' => array( 'entry_options' => array(
'data' => 'bar', 'data' => 'bar',
), ),
)); ));

View File

@ -347,8 +347,8 @@ class FormTypeCsrfExtensionTest extends TypeTestCase
{ {
$prototypeView = $this->factory $prototypeView = $this->factory
->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', null, array( ->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', null, array(
'type' => __CLASS__.'_ChildType', 'entry_type' => __CLASS__.'_ChildType',
'options' => array( 'entry_options' => array(
'csrf_field_name' => 'csrf', 'csrf_field_name' => 'csrf',
), ),
'prototype' => true, 'prototype' => true,