Skip to content

Commit

Permalink
bug #13170 [Form] Set a child type to text if added to the form witho…
Browse files Browse the repository at this point in the history
…ut 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.
  • Loading branch information
fabpot committed Jan 9, 2015
2 parents 4253854 + 57070a2 commit d5e9de2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Symfony/Component/Form/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,10 @@ public function add($child, $type = null, array $options = array())
// 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 {
Expand Down
16 changes: 16 additions & 0 deletions src/Symfony/Component/Form/Tests/CompoundFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,22 @@ public function testAddUsingIntegerNameAndType()
$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')
Expand Down

0 comments on commit d5e9de2

Please sign in to comment.