Skip to content
Permalink
Browse files

Fix error where checkboxes could not be unchecked.

Sending checked=false should be honoured.

Fixes #3995
  • Loading branch information...
markstory committed Jul 16, 2014
1 parent a2b9e74 commit 6911d4e565d4419c11f1baba198ff7ce6646ced2
Showing with 42 additions and 3 deletions.
  1. +2 −3 src/View/Widget/Checkbox.php
  2. +40 −0 tests/TestCase/View/Widget/CheckboxTest.php
@@ -60,7 +60,6 @@ public function render(array $data, ContextInterface $context) {
'name' => '',
'value' => 1,
'val' => null,
'checked' => false,
'disabled' => false,
];
if ($this->_isChecked($data)) {
@@ -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;
@@ -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.
You can’t perform that action at this time.