8000 [Form] Allowed binding false to a checkbox by jakzal · Pull Request #7789 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Allowed binding false to a checkbox #7789

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 22, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Symfony/Component/Form/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -488,11 +488,14 @@ public function bind($submittedData)
$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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,28 @@ public function testBindWithEmptyValueUnchecked()
$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
*/
Expand Down
22 changes: 22 additions & 0 deletions src/Symfony/Component/Form/Tests/SimpleFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,28 @@ public function testDataIsInitializedFromBind()
$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
*/
Expand Down
0