Skip to content

Commit

Permalink
Rename CollectionType options for entries
Browse files Browse the repository at this point in the history
  • Loading branch information
wouterj committed Oct 10, 2015
1 parent 2d64cf2 commit a52672b
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 34 deletions.
45 changes: 38 additions & 7 deletions Extension/Core/Type/CollectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ class CollectionType extends AbstractType
public function buildForm(FormBuilderInterface $builder, array $options)
{
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__',
), $options['options'], array(
), $options['entry_options'], array(
'data' => $options['prototype_data'],
)));
$builder->setAttribute('prototype', $prototype->getForm());
}

$resizeListener = new ResizeFormListener(
$options['type'],
$options['options'],
$options['entry_type'],
$options['entry_options'],
$options['allow_add'],
$options['allow_delete'],
$options['delete_empty']
Expand Down Expand Up @@ -76,24 +76,55 @@ public function finishView(FormView $view, FormInterface $form, array $options)
*/
public function configureOptions(OptionsResolver $resolver)
{
$optionsNormalizer = function (Options $options, $value) {
$entryOptionsNormalizer = function (Options $options, $value) {
$value['block_name'] = 'entry';

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(
'allow_add' => false,
'allow_delete' => false,
'prototype' => true,
'prototype_data' => null,
'prototype_name' => '__name__',
'type' => __NAMESPACE__.'\TextType',
'options' => array(),
// deprecated as of Symfony 2.8, to be removed in Symfony 3.0. Use entry_type instead
'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,
));

$resolver->setNormalizer('options', $optionsNormalizer);
$resolver->setNormalizer('entry_options', $entryOptionsNormalizer);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions Tests/AbstractDivLayoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ public function testRestAndRepeatedWithWidgetPerChild()
public function testCollection()
{
$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(),
Expand All @@ -306,7 +306,7 @@ public function testCollectionWithAlternatingRowTypes()
array('title' => 'b'),
);
$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(),
Expand All @@ -324,7 +324,7 @@ public function testCollectionWithAlternatingRowTypes()
public function testEmptyCollection()
{
$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(),
Expand All @@ -341,7 +341,7 @@ public function testCollectionRow()
'collection',
'Symfony\Component\Form\Extension\Core\Type\CollectionType',
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')
Expand Down
58 changes: 37 additions & 21 deletions Tests/Extension/Core/Type/CollectionTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class CollectionTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
public function testLegacyName()
{
$form = $this->factory->create('collection', array(
'type' => 'text',
'entry_type' => 'text',
));

$this->assertSame('collection', $form->getConfig()->getType()->getName());
Expand All @@ -40,8 +40,8 @@ public function testContainsNoChildByDefault()
public function testSetDataAdjustsSize()
{
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', null, array(
'type' => 'Symfony\Component\Form\Extension\Core\Type\TextType',
'options' => array(
'entry_type' => 'Symfony\Component\Form\Extension\Core\Type\TextType',
'entry_options' => array(
'attr' => array('maxlength' => 20),
),
));
Expand Down Expand Up @@ -69,7 +69,7 @@ public function testSetDataAdjustsSize()
public function testThrowsExceptionIfObjectIsNotTraversable()
{
$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');
$form->setData(new \stdClass());
Expand All @@ -78,7 +78,7 @@ public function testThrowsExceptionIfObjectIsNotTraversable()
public function testNotResizedIfSubmittedWithMissingData()
{
$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('[email protected]', '[email protected]'));
$form->submit(array('[email protected]'));
Expand All @@ -92,7 +92,7 @@ public function testNotResizedIfSubmittedWithMissingData()
public function testResizedDownIfSubmittedWithMissingDataAndAllowDelete()
{
$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,
));
$form->setData(array('[email protected]', '[email protected]'));
Expand All @@ -107,7 +107,7 @@ public function testResizedDownIfSubmittedWithMissingDataAndAllowDelete()
public function testResizedDownIfSubmittedWithEmptyDataAndDeleteEmpty()
{
$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,
'delete_empty' => true,
));
Expand All @@ -124,7 +124,7 @@ public function testResizedDownIfSubmittedWithEmptyDataAndDeleteEmpty()
public function testDontAddEmptyDataIfDeleteEmpty()
{
$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,
'delete_empty' => true,
));
Expand All @@ -141,7 +141,7 @@ public function testDontAddEmptyDataIfDeleteEmpty()
public function testNoDeleteEmptyIfDeleteNotAllowed()
{
$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,
'delete_empty' => true,
));
Expand All @@ -156,10 +156,10 @@ public function testNoDeleteEmptyIfDeleteNotAllowed()
public function testResizedDownIfSubmittedWithCompoundEmptyDataAndDeleteEmpty()
{
$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
// form is completely empty
'options' => array('required' => false),
'entry_options' => array('required' => false),
'allow_add' => true,
'delete_empty' => true,
));
Expand All @@ -179,7 +179,7 @@ public function testResizedDownIfSubmittedWithCompoundEmptyDataAndDeleteEmpty()
public function testNotResizedIfSubmittedWithExtraData()
{
$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('[email protected]'));
$form->submit(array('[email protected]', '[email protected]'));
Expand All @@ -192,7 +192,7 @@ public function testNotResizedIfSubmittedWithExtraData()
public function testResizedUpIfSubmittedWithExtraDataAndAllowAdd()
{
$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,
));
$form->setData(array('[email protected]'));
Expand All @@ -208,7 +208,7 @@ public function testResizedUpIfSubmittedWithExtraDataAndAllowAdd()
public function testAllowAddButNoPrototype()
{
$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,
'prototype' => false,
));
Expand All @@ -220,7 +220,7 @@ public function testPrototypeMultipartPropagation()
{
$form = $this->factory
->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,
'prototype' => true,
))
Expand All @@ -232,7 +232,7 @@ public function testPrototypeMultipartPropagation()
public function testGetDataDoesNotContainsPrototypeNameBeforeDataAreSet()
{
$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,
'allow_add' => true,
));
Expand All @@ -244,7 +244,7 @@ public function testGetDataDoesNotContainsPrototypeNameBeforeDataAreSet()
public function testGetDataDoesNotContainsPrototypeNameAfterDataAreSet()
{
$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,
'prototype' => true,
));
Expand All @@ -257,15 +257,15 @@ public function testGetDataDoesNotContainsPrototypeNameAfterDataAreSet()
public function testPrototypeNameOption()
{
$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,
'allow_add' => true,
));

$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(
'type' => 'Symfony\Component\Form\Extension\Core\Type\FormType',
'entry_type' => 'Symfony\Component\Form\Extension\Core\Type\FormType',
'prototype' => true,
'allow_add' => true,
'prototype_name' => '__test__',
Expand All @@ -274,10 +274,26 @@ public function testPrototypeNameOption()
$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()
{
$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,
'prototype' => true,
'prototype_name' => '__test__',
Expand All @@ -293,7 +309,7 @@ public function testPrototypeData()
'allow_add' => true,
'prototype' => true,
'prototype_data' => 'foo',
'options' => array(
'entry_options' => array(
'data' => 'bar',
),
));
Expand Down
4 changes: 2 additions & 2 deletions Tests/Extension/Csrf/Type/FormTypeCsrfExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,8 @@ public function testNoCsrfProtectionOnPrototype()
{
$prototypeView = $this->factory
->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', null, array(
'type' => __CLASS__.'_ChildType',
'options' => array(
'entry_type' => __CLASS__.'_ChildType',
'entry_options' => array(
'csrf_field_name' => 'csrf',
),
'prototype' => true,
Expand Down

0 comments on commit a52672b

Please sign in to comment.