[Form] Fix for treatment zero as empty data. Closes #1986

This commit is contained in:
stloyd 2011-08-22 15:19:31 +02:00
parent 12a852e18b
commit c29fa9d64e
2 changed files with 31 additions and 1 deletions

View File

@ -655,7 +655,7 @@ class FormBuilder
$instance->add($child);
}
if ($this->getData()) {
if (null !== $this->getData()) {
$instance->setData($this->getData());
}

View File

@ -203,6 +203,36 @@ class FieldTypeTest extends TypeTestCase
$this->assertEquals(0, count($form->getAttribute('attr')));
}
/**
* @see https://github.com/symfony/symfony/issues/1986
*/
public function testSetDataThroughParamsWithZero()
{
$form = $this->factory->create('field', null, array('data' => 0));
$view = $form->createView();
$this->assertFalse($form->isEmpty());
$this->assertSame('0', $view->get('value'));
$this->assertSame('0', $form->getData());
$form = $this->factory->create('field', null, array('data' => '0'));
$view = $form->createView();
$this->assertFalse($form->isEmpty());
$this->assertSame('0', $view->get('value'));
$this->assertSame('0', $form->getData());
$form = $this->factory->create('field', null, array('data' => '00000'));
$view = $form->createView();
$this->assertFalse($form->isEmpty());
$this->assertSame('00000', $view->get('value'));
$this->assertSame('00000', $form->getData());
}
/**
* @expectedException Symfony\Component\Form\Exception\FormException
*/