Skip to content

Commit 30f65c7

Browse files
committed
bug #13170 [Form] Set a child type to text if added to the form without a type. (jakzal)
This PR was merged into the 2.3 branch. Discussion ---------- [Form] Set a child type to text if added to the form without a type. | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #13167 | License | MIT | Doc PR | - This copies the behaviour of `FormBuilder::create()` to `Form::add()`. ping @webmozart Commits ------- 57070a2 [Form] Set a child type to text if added to the form without a type.
2 parents 16085b7 + 79af0f3 commit 30f65c7

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

Form.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,10 @@ public function add($child, $type = null, array $options = array())
873873
// Never initialize child forms automatically
874874
$options['auto_initialize'] = false;
875875

876+
if (null === $type && null === $this->config->getDataClass()) {
877+
$type = 'text';
878+
}
879+
876880
if (null === $type) {
877881
$child = $this->config->getFormFactory()->createForProperty($this->config->getDataClass(), $child, null, $options);
878882
} else {

Tests/CompoundFormTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,22 @@ public function testAddUsingIntegerNameAndType()
207207
$this->assertSame(array(0 => $child), $this->form->all());
208208
}
209209

210+
public function testAddWithoutType()
211+
{
212+
$child = $this->getBuilder('foo')->getForm();
213+
214+
$this->factory->expects($this->once())
215+
->method('createNamed')
216+
->with('foo', 'text')
217+
->will($this->returnValue($child));
218+
219+
$this->form->add('foo');
220+
221+
$this->assertTrue($this->form->has('foo'));
222+
$this->assertSame($this->form, $child->getParent());
223+
$this->assertSame(array('foo' => $child), $this->form->all());
224+
}
225+
210226
public function testAddUsingNameButNoType()
211227
{
212228
$this->form = $this->getBuilder('name', null, '\stdClass')

0 commit comments

Comments
 (0)