Browse Source

Fix error where checkboxes could not be unchecked.

Sending checked=false should be honoured.

Fixes #3995
mark_story 11 years ago
parent
commit
6911d4e565
2 changed files with 42 additions and 3 deletions
  1. 2 3
      src/View/Widget/Checkbox.php
  2. 40 0
      tests/TestCase/View/Widget/CheckboxTest.php

+ 2 - 3
src/View/Widget/Checkbox.php

@@ -60,7 +60,6 @@ class Checkbox implements WidgetInterface {
 			'name' => '',
 			'value' => 1,
 			'val' => null,
-			'checked' => false,
 			'disabled' => false,
 		];
 		if ($this->_isChecked($data)) {
@@ -87,8 +86,8 @@ class Checkbox implements WidgetInterface {
  * @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;

+ 40 - 0
tests/TestCase/View/Widget/CheckboxTest.php

@@ -185,4 +185,44 @@ class CheckboxTest extends TestCase {
 		$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);
+	}
+
 }