From 57070a22471f284ac0eebb0d1176d2d5923e6c3a Mon Sep 17 00:00:00 2001 From: Jakub Zalas Date: Tue, 30 Dec 2014 11:40:10 +0000 Subject: [PATCH] [Form] Set a child type to text if added to the form without a type. This copies the behaviour of the FormBuilder::create() to the Form::add(). --- src/Symfony/Component/Form/Form.php | 4 ++++ .../Component/Form/Tests/CompoundFormTest.php | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/Symfony/Component/Form/Form.php b/src/Symfony/Component/Form/Form.php index b67700a6f3..8a19f82a2d 100644 --- a/src/Symfony/Component/Form/Form.php +++ b/src/Symfony/Component/Form/Form.php @@ -873,6 +873,10 @@ class Form implements \IteratorAggregate, FormInterface // Never initialize child forms automatically $options['auto_initialize'] = false; + if (null === $type && null === $this->config->getDataClass()) { + $type = 'text'; + } + if (null === $type) { $child = $this->config->getFormFactory()->createForProperty($this->config->getDataClass(), $child, null, $options); } else { diff --git a/src/Symfony/Component/Form/Tests/CompoundFormTest.php b/src/Symfony/Component/Form/Tests/CompoundFormTest.php index 17c4f3b0bf..6e500baaa9 100644 --- a/src/Symfony/Component/Form/Tests/CompoundFormTest.php +++ b/src/Symfony/Component/Form/Tests/CompoundFormTest.php @@ -207,6 +207,22 @@ class CompoundFormTest extends AbstractFormTest $this->assertSame(array(0 => $child), $this->form->all()); } + public function testAddWithoutType() + { + $child = $this->getBuilder('foo')->getForm(); + + $this->factory->expects($this->once()) + ->method('createNamed') + ->with('foo', 'text') + ->will($this->returnValue($child)); + + $this->form->add('foo'); + + $this->assertTrue($this->form->has('foo')); + $this->assertSame($this->form, $child->getParent()); + $this->assertSame(array('foo' => $child), $this->form->all()); + } + public function testAddUsingNameButNoType() { $this->form = $this->getBuilder('name', null, '\stdClass')