merged branch jakzal/checkbox-type-false (PR #7789)

This PR was merged into the master branch.

Discussion
----------

[Form] Allowed binding false to a checkbox

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | yes
| Deprecations? | no
| Tests pass?   | [![Build Status](https://travis-ci.org/jakzal/symfony.png?branch=checkbox-type-false)](https://travis-ci.org/jakzal/symfony)
| Fixed tickets | #7139
| License       | MIT
| Doc PR        | -

Commits
-------

24ef8d2 [Form] Added a SimpleFormTest test case.
e493984 [Form] Allowed binding false to a checkbox.
This commit is contained in:
Fabien Potencier 2013-04-22 20:30:59 +02:00
commit 9cdd1c7b6f
3 changed files with 48 additions and 1 deletions

View File

@ -488,11 +488,14 @@ class Form implements \IteratorAggregate, FormInterface
$this->setData($this->config->getData());
}
// Treat false as NULL to support binding false to checkboxes.
// Don't convert NULL to a string here in order to determine later
// whether an empty value has been submitted or whether no value has
// been submitted at all. This is important for processing checkboxes
// and radio buttons with empty values.
if (is_scalar($submittedData)) {
if (false === $submittedData) {
$submittedData = null;
} elseif (is_scalar($submittedData)) {
$submittedData = (string) $submittedData;
}

View File

@ -105,6 +105,28 @@ class CheckboxTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
$this->assertNull($form->getViewData());
}
public function testBindWithEmptyValueAndFalseUnchecked()
{
$form = $this->factory->create('checkbox', null, array(
'value' => '',
));
$form->bind(false);
$this->assertFalse($form->getData());
$this->assertNull($form->getViewData());
}
public function testBindWithEmptyValueAndTrueChecked()
{
$form = $this->factory->create('checkbox', null, array(
'value' => '',
));
$form->bind(true);
$this->assertTrue($form->getData());
$this->assertSame('', $form->getViewData());
}
/**
* @dataProvider provideTransformedData
*/

View File

@ -96,6 +96,28 @@ class SimpleFormTest extends AbstractFormTest
$form->bind('foobar');
}
// https://github.com/symfony/symfony/pull/7789
public function testFalseIsConvertedToNull()
{
$mock = $this->getMockBuilder('\stdClass')
->setMethods(array('preBind'))
->getMock();
$mock->expects($this->once())
->method('preBind')
->with($this->callback(function ($event) {
return null === $event->getData();
}));
$config = new FormConfigBuilder('name', null, $this->dispatcher);
$config->addEventListener(FormEvents::PRE_BIND, array($mock, 'preBind'));
$form = new Form($config);
$form->bind(false);
$this->assertTrue($form->isValid());
$this->assertNull($form->getData());
}
/**
* @expectedException \Symfony\Component\Form\Exception\AlreadyBoundException
*/