From b5277752b73882bbb1159e2fa7e32e93f187d5d0 Mon Sep 17 00:00:00 2001 From: Jeremie Augustin Date: Thu, 28 Apr 2011 19:17:16 +0200 Subject: [PATCH 1/5] [Form] add type_options for CollectionType to be abble to set options to type --- .../Core/EventListener/ResizeFormListener.php | 18 ++++++++++++------ .../Extension/Core/Type/CollectionType.php | 7 ++++--- 2 files changed, 16 insertions(+), 9 deletions(-) mode change 100644 => 100755 src/Symfony/Component/Form/Extension/Core/EventListener/ResizeFormListener.php mode change 100644 => 100755 src/Symfony/Component/Form/Extension/Core/Type/CollectionType.php diff --git a/src/Symfony/Component/Form/Extension/Core/EventListener/ResizeFormListener.php b/src/Symfony/Component/Form/Extension/Core/EventListener/ResizeFormListener.php old mode 100644 new mode 100755 index 9746167bc7..d2b064a835 --- a/src/Symfony/Component/Form/Extension/Core/EventListener/ResizeFormListener.php +++ b/src/Symfony/Component/Form/Extension/Core/EventListener/ResizeFormListener.php @@ -40,12 +40,18 @@ class ResizeFormListener implements EventSubscriberInterface */ private $allowAdd; - public function __construct(FormFactoryInterface $factory, $type, $allowAdd = false, $allowDelete = false) + /** + * @var array + */ + private $typeOptions; + + public function __construct(FormFactoryInterface $factory, $type, $allowAdd = false, $allowDelete = false, array $typeOptions = array()) { $this->factory = $factory; $this->type = $type; $this->allowAdd = $allowAdd; $this->allowDelete = $allowDelete; + $this->typeOptions = $typeOptions; } public static function getSubscribedEvents() @@ -79,9 +85,9 @@ class ResizeFormListener implements EventSubscriberInterface // Then add all rows again in the correct order foreach ($data as $name => $value) { - $form->add($this->factory->createNamed($this->type, $name, null, array( + $form->add($this->factory->createNamed($this->type, $name, null, array_merge(array( 'property_path' => '['.$name.']', - ))); + ), $this->typeOptions))); } } @@ -111,9 +117,9 @@ class ResizeFormListener implements EventSubscriberInterface if ($this->allowAdd) { foreach ($data as $name => $value) { if (!$form->has($name)) { - $form->add($this->factory->createNamed($this->type, $name, null, array( + $form->add($this->factory->createNamed($this->type, $name, null, array_merge(array( 'property_path' => '['.$name.']', - ))); + ), $this->typeOptions))); } } } @@ -142,4 +148,4 @@ class ResizeFormListener implements EventSubscriberInterface $event->setData($data); } -} \ No newline at end of file +} diff --git a/src/Symfony/Component/Form/Extension/Core/Type/CollectionType.php b/src/Symfony/Component/Form/Extension/Core/Type/CollectionType.php old mode 100644 new mode 100755 index 45dc7aa0f5..92ef9697e7 --- a/src/Symfony/Component/Form/Extension/Core/Type/CollectionType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/CollectionType.php @@ -22,14 +22,14 @@ class CollectionType extends AbstractType public function buildForm(FormBuilder $builder, array $options) { if ($options['allow_add'] && $options['prototype']) { - $builder->add('$$name$$', $options['type'], array( + $builder->add('$$name$$', $options['type'], array_merge(array( 'property_path' => false, 'required' => false, - )); + ), $options['type_options'])); } $listener = new ResizeFormListener($builder->getFormFactory(), - $options['type'], $options['allow_add'], $options['allow_delete']); + $options['type'], $options['allow_add'], $options['allow_delete'], $options['type_options']); $builder->addEventSubscriber($listener) ->setAttribute('allow_add', $options['allow_add']) @@ -49,6 +49,7 @@ class CollectionType extends AbstractType 'allow_delete' => false, 'prototype' => true, 'type' => 'text', + 'type_options' => array(), ); } From 0077963584fdf33248f3d4880c9421928755d34b Mon Sep 17 00:00:00 2001 From: Jeremie Augustin Date: Fri, 29 Apr 2011 09:20:17 +0200 Subject: [PATCH 2/5] fix file permissions to 644 --- .../Form/Extension/Core/EventListener/ResizeFormListener.php | 0 src/Symfony/Component/Form/Extension/Core/Type/CollectionType.php | 0 2 files changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 src/Symfony/Component/Form/Extension/Core/EventListener/ResizeFormListener.php mode change 100755 => 100644 src/Symfony/Component/Form/Extension/Core/Type/CollectionType.php diff --git a/src/Symfony/Component/Form/Extension/Core/EventListener/ResizeFormListener.php b/src/Symfony/Component/Form/Extension/Core/EventListener/ResizeFormListener.php old mode 100755 new mode 100644 diff --git a/src/Symfony/Component/Form/Extension/Core/Type/CollectionType.php b/src/Symfony/Component/Form/Extension/Core/Type/CollectionType.php old mode 100755 new mode 100644 From fb0a37f9baab09a8b513e035e87ce12dc411ed9a Mon Sep 17 00:00:00 2001 From: Jeremie Augustin Date: Wed, 11 May 2011 10:43:09 +0200 Subject: [PATCH 3/5] [Form] add tests for type_options in collectionType --- .../Core/Type/CollectionTypeTest.php | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) mode change 100644 => 100755 tests/Symfony/Tests/Component/Form/Extension/Core/Type/CollectionTypeTest.php diff --git a/tests/Symfony/Tests/Component/Form/Extension/Core/Type/CollectionTypeTest.php b/tests/Symfony/Tests/Component/Form/Extension/Core/Type/CollectionTypeTest.php old mode 100644 new mode 100755 index 7a2b89eda3..2193c6306f --- a/tests/Symfony/Tests/Component/Form/Extension/Core/Type/CollectionTypeTest.php +++ b/tests/Symfony/Tests/Component/Form/Extension/Core/Type/CollectionTypeTest.php @@ -143,4 +143,91 @@ class CollectionFormTest extends TypeTestCase $this->assertFalse($form->has('$$name$$')); } + + public function testSetTypeOptions() + { + $form = $this->factory->create('collection', null, array( + 'type' => 'field', + 'type_options' => array( + 'required' => false, + 'max_length' => 20 + ), + )); + $form->setData(array('foo@foo.com', 'foo@bar.com')); + + $this->assertEquals(false, $form[0]->isRequired()); + $this->assertEquals(false, $form[1]->isRequired()); + $this->assertEquals(20, $form[0]->getAttribute('max_length')); + $this->assertEquals(20, $form[1]->getAttribute('max_length')); + + $form->bind(array('foo@bar.com', 'bar@foo.com')); + + $this->assertEquals(false, $form[0]->isRequired()); + $this->assertEquals(false, $form[1]->isRequired()); + $this->assertEquals(20, $form[0]->getAttribute('max_length')); + $this->assertEquals(20, $form[1]->getAttribute('max_length')); + + //Test with prototype and extra field + $form = $this->factory->create('collection', null, array( + 'allow_add' => true, + 'prototype' => true, + 'type' => 'field', + 'type_options' => array( + 'required' => false, + 'max_length' => 20 + ), + )); + + $form->setData(array('foo@foo.com')); + + $this->assertEquals(false, $form[0]->isRequired()); + $this->assertEquals(20, $form[0]->getAttribute('max_length')); + + $form->bind(array('foo@bar.com', 'bar@foo.com')); + + $this->assertEquals(false, $form[0]->isRequired()); + $this->assertEquals(false, $form[1]->isRequired()); + $this->assertEquals(20, $form[0]->getAttribute('max_length')); + $this->assertEquals(20, $form[1]->getAttribute('max_length')); + + } + + public function testSetTypeOptionsWithoutOptions() + { + $form = $this->factory->create('collection', null, array( + 'type' => 'field', + )); + $form->setData(array('foo@foo.com', 'foo@bar.com')); + + $this->assertEquals(true, $form[0]->isRequired()); + $this->assertEquals(true, $form[1]->isRequired()); + $this->assertEquals(null, $form[0]->getAttribute('max_length')); + $this->assertEquals(null, $form[1]->getAttribute('max_length')); + + $form->bind(array('foo@bar.com', 'bar@foo.com')); + + $this->assertEquals(true, $form[0]->isRequired()); + $this->assertEquals(true, $form[1]->isRequired()); + $this->assertEquals(null, $form[0]->getAttribute('max_length')); + $this->assertEquals(null, $form[1]->getAttribute('max_length')); + + //Test with prototype and extra field + $form = $this->factory->create('collection', null, array( + 'allow_add' => true, + 'prototype' => true, + 'type' => 'field', + )); + + $form->setData(array('foo@foo.com')); + + $this->assertEquals(true, $form[0]->isRequired()); + $this->assertEquals(null, $form[0]->getAttribute('max_length')); + + $form->bind(array('foo@bar.com', 'bar@foo.com')); + + $this->assertEquals(true, $form[0]->isRequired()); + $this->assertEquals(true, $form[1]->isRequired()); + $this->assertEquals(null, $form[0]->getAttribute('max_length')); + $this->assertEquals(null, $form[1]->getAttribute('max_length')); + } } From d3db844cfa9a659b1d37e8b3ed6fa21d5687af18 Mon Sep 17 00:00:00 2001 From: Jeremie Augustin Date: Wed, 11 May 2011 10:54:42 +0200 Subject: [PATCH 4/5] [Form] fix file permissions to 644 again ;) --- .../Component/Form/Extension/Core/Type/CollectionTypeTest.php | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 tests/Symfony/Tests/Component/Form/Extension/Core/Type/CollectionTypeTest.php diff --git a/tests/Symfony/Tests/Component/Form/Extension/Core/Type/CollectionTypeTest.php b/tests/Symfony/Tests/Component/Form/Extension/Core/Type/CollectionTypeTest.php old mode 100755 new mode 100644 From bea448339b8134e2e69f129c42fda52080507a0e Mon Sep 17 00:00:00 2001 From: Jeremie Augustin Date: Wed, 11 May 2011 11:18:01 +0200 Subject: [PATCH 5/5] [Form] replace assertEquals by assertFalse, assertTrue, assertNull --- .../Core/Type/CollectionTypeTest.php | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/tests/Symfony/Tests/Component/Form/Extension/Core/Type/CollectionTypeTest.php b/tests/Symfony/Tests/Component/Form/Extension/Core/Type/CollectionTypeTest.php index 2193c6306f..253828fdb1 100644 --- a/tests/Symfony/Tests/Component/Form/Extension/Core/Type/CollectionTypeTest.php +++ b/tests/Symfony/Tests/Component/Form/Extension/Core/Type/CollectionTypeTest.php @@ -155,15 +155,15 @@ class CollectionFormTest extends TypeTestCase )); $form->setData(array('foo@foo.com', 'foo@bar.com')); - $this->assertEquals(false, $form[0]->isRequired()); - $this->assertEquals(false, $form[1]->isRequired()); + $this->assertFalse($form[0]->isRequired()); + $this->assertFalse($form[1]->isRequired()); $this->assertEquals(20, $form[0]->getAttribute('max_length')); $this->assertEquals(20, $form[1]->getAttribute('max_length')); $form->bind(array('foo@bar.com', 'bar@foo.com')); - $this->assertEquals(false, $form[0]->isRequired()); - $this->assertEquals(false, $form[1]->isRequired()); + $this->assertFalse($form[0]->isRequired()); + $this->assertFalse($form[1]->isRequired()); $this->assertEquals(20, $form[0]->getAttribute('max_length')); $this->assertEquals(20, $form[1]->getAttribute('max_length')); @@ -180,13 +180,13 @@ class CollectionFormTest extends TypeTestCase $form->setData(array('foo@foo.com')); - $this->assertEquals(false, $form[0]->isRequired()); + $this->assertFalse($form[0]->isRequired()); $this->assertEquals(20, $form[0]->getAttribute('max_length')); $form->bind(array('foo@bar.com', 'bar@foo.com')); - $this->assertEquals(false, $form[0]->isRequired()); - $this->assertEquals(false, $form[1]->isRequired()); + $this->assertFalse($form[0]->isRequired()); + $this->assertFalse($form[1]->isRequired()); $this->assertEquals(20, $form[0]->getAttribute('max_length')); $this->assertEquals(20, $form[1]->getAttribute('max_length')); @@ -199,17 +199,17 @@ class CollectionFormTest extends TypeTestCase )); $form->setData(array('foo@foo.com', 'foo@bar.com')); - $this->assertEquals(true, $form[0]->isRequired()); - $this->assertEquals(true, $form[1]->isRequired()); - $this->assertEquals(null, $form[0]->getAttribute('max_length')); - $this->assertEquals(null, $form[1]->getAttribute('max_length')); + $this->assertTrue($form[0]->isRequired()); + $this->assertTrue($form[1]->isRequired()); + $this->assertNull($form[0]->getAttribute('max_length')); + $this->assertNull($form[1]->getAttribute('max_length')); $form->bind(array('foo@bar.com', 'bar@foo.com')); - $this->assertEquals(true, $form[0]->isRequired()); - $this->assertEquals(true, $form[1]->isRequired()); - $this->assertEquals(null, $form[0]->getAttribute('max_length')); - $this->assertEquals(null, $form[1]->getAttribute('max_length')); + $this->assertTrue($form[0]->isRequired()); + $this->assertTrue($form[1]->isRequired()); + $this->assertNull($form[0]->getAttribute('max_length')); + $this->assertNull($form[1]->getAttribute('max_length')); //Test with prototype and extra field $form = $this->factory->create('collection', null, array( @@ -220,14 +220,14 @@ class CollectionFormTest extends TypeTestCase $form->setData(array('foo@foo.com')); - $this->assertEquals(true, $form[0]->isRequired()); - $this->assertEquals(null, $form[0]->getAttribute('max_length')); + $this->assertTrue($form[0]->isRequired()); + $this->assertNull($form[0]->getAttribute('max_length')); $form->bind(array('foo@bar.com', 'bar@foo.com')); - $this->assertEquals(true, $form[0]->isRequired()); - $this->assertEquals(true, $form[1]->isRequired()); - $this->assertEquals(null, $form[0]->getAttribute('max_length')); - $this->assertEquals(null, $form[1]->getAttribute('max_length')); + $this->assertTrue($form[0]->isRequired()); + $this->assertTrue($form[1]->isRequired()); + $this->assertNull($form[0]->getAttribute('max_length')); + $this->assertNull($form[1]->getAttribute('max_length')); } }