Skip to content

Commit

Permalink
Fix error where checkboxes could not be unchecked.
Browse files Browse the repository at this point in the history
Sending checked=false should be honoured.

Fixes #3995
  • Loading branch information
markstory committed Jul 16, 2014
1 parent a2b9e74 commit 6911d4e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/View/Widget/Checkbox.php
Expand Up @@ -60,7 +60,6 @@ public function render(array $data, ContextInterface $context) {
'name' => '',
'value' => 1,
'val' => null,
'checked' => false,
'disabled' => false,
];
if ($this->_isChecked($data)) {
Expand All @@ -87,8 +86,8 @@ public function render(array $data, ContextInterface $context) {
* @return bool
*/
protected function _isChecked($data) {
if (!empty($data['checked'])) {
return true;
if (array_key_exists('checked', $data)) {
return (bool)$data['checked'];
}
if ((string)$data['val'] === (string)$data['value']) {
return true;
Expand Down
40 changes: 40 additions & 0 deletions tests/TestCase/View/Widget/CheckboxTest.php
Expand Up @@ -185,4 +185,44 @@ public function testRenderCheckedValue($checked) {
$this->assertTags($result, $expected);
}

/**
* Data provider for checkbox values
*
* @return array
*/
public static function uncheckedProvider() {
return [
[''],
['0'],
[0],
[false],
[null],
];
}

/**
* Test rendering unchecked checkboxes
*
* @dataProvider uncheckedProvider
* @return void
*/
public function testRenderUnCheckedValue($checked) {
$checkbox = new Checkbox($this->templates);
$data = [
'name' => 'Comment[spam]',
'value' => 1,
'val' => 1,
'checked' => $checked,
];
$result = $checkbox->render($data, $this->context);
$expected = [
'input' => [
'type' => 'checkbox',
'name' => 'Comment[spam]',
'value' => 1,
]
];
$this->assertTags($result, $expected);
}

}

0 comments on commit 6911d4e

Please sign in to comment.