From 942a2370c6a436bfe138f49ae18b4756fc710301 Mon Sep 17 00:00:00 2001 From: WouterJ Date: Sun, 1 Mar 2015 23:32:42 +0100 Subject: [PATCH] Rename CollectionType options for entries --- .../Extension/Core/Type/CollectionType.php | 45 +++++++++++--- .../Form/Tests/AbstractDivLayoutTest.php | 8 +-- .../Core/Type/CollectionTypeTest.php | 58 ++++++++++++------- .../Csrf/Type/FormTypeCsrfExtensionTest.php | 4 +- 4 files changed, 81 insertions(+), 34 deletions(-) diff --git a/src/Symfony/Component/Form/Extension/Core/Type/CollectionType.php b/src/Symfony/Component/Form/Extension/Core/Type/CollectionType.php index 508013b312ed3..a779df619261b 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/CollectionType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/CollectionType.php @@ -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'] @@ -76,11 +76,37 @@ 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, @@ -88,12 +114,17 @@ public function configureOptions(OptionsResolver $resolver) '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); } /** diff --git a/src/Symfony/Component/Form/Tests/AbstractDivLayoutTest.php b/src/Symfony/Component/Form/Tests/AbstractDivLayoutTest.php index fb7be87eadfe0..30a8225f144b3 100644 --- a/src/Symfony/Component/Form/Tests/AbstractDivLayoutTest.php +++ b/src/Symfony/Component/Form/Tests/AbstractDivLayoutTest.php @@ -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(), @@ -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(), @@ -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(), @@ -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') diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/CollectionTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/CollectionTypeTest.php index 81010ae50f371..cc00945f913e7 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/CollectionTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/CollectionTypeTest.php @@ -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()); @@ -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), ), )); @@ -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()); @@ -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('foo@foo.com', 'bar@bar.com')); $form->submit(array('foo@bar.com')); @@ -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('foo@foo.com', 'bar@bar.com')); @@ -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, )); @@ -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, )); @@ -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, )); @@ -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, )); @@ -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('foo@bar.com')); $form->submit(array('foo@foo.com', 'bar@bar.com')); @@ -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('foo@bar.com')); @@ -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, )); @@ -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, )) @@ -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, )); @@ -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, )); @@ -257,7 +257,7 @@ 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, )); @@ -265,7 +265,7 @@ public function testPrototypeNameOption() $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__', @@ -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__', @@ -293,7 +309,7 @@ public function testPrototypeData() 'allow_add' => true, 'prototype' => true, 'prototype_data' => 'foo', - 'options' => array( + 'entry_options' => array( 'data' => 'bar', ), )); diff --git a/src/Symfony/Component/Form/Tests/Extension/Csrf/Type/FormTypeCsrfExtensionTest.php b/src/Symfony/Component/Form/Tests/Extension/Csrf/Type/FormTypeCsrfExtensionTest.php index 1a1e15b53fe8f..3a34394070919 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Csrf/Type/FormTypeCsrfExtensionTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Csrf/Type/FormTypeCsrfExtensionTest.php @@ -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, pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy