From b8780586ec9f04ff245c5772d4f36919ec6148bf Mon Sep 17 00:00:00 2001 From: mark_story Date: Fri, 14 Jan 2011 21:48:23 -0500 Subject: [PATCH] Fixing issue where FormHelper::checkbox() would ignore an explicit checked = false, and use the post data instead. Test case added. Fixes #1437 --- cake/libs/view/helpers/form.php | 5 +- .../cases/libs/view/helpers/form.test.php | 64 +++++++++++++------ 2 files changed, 47 insertions(+), 22 deletions(-) diff --git a/cake/libs/view/helpers/form.php b/cake/libs/view/helpers/form.php index 5ee8a6c2c1d..b62092ed466 100644 --- a/cake/libs/view/helpers/form.php +++ b/cake/libs/view/helpers/form.php @@ -1007,7 +1007,10 @@ function checkbox($fieldName, $options = array()) { if (empty($options['value'])) { $options['value'] = 1; - } elseif (!empty($value) && $value === $options['value']) { + } elseif ( + (!isset($options['checked']) && !empty($value) && $value === $options['value']) || + !empty($options['checked']) + ) { $options['checked'] = 'checked'; } if ($options['hiddenField']) { diff --git a/cake/tests/cases/libs/view/helpers/form.test.php b/cake/tests/cases/libs/view/helpers/form.test.php index 3a9868c7714..3c4cea94029 100644 --- a/cake/tests/cases/libs/view/helpers/form.test.php +++ b/cake/tests/cases/libs/view/helpers/form.test.php @@ -3773,27 +3773,6 @@ function testCheckbox() { ); $this->assertTags($result, $expected); - $result = $this->Form->checkbox('Model.field', array('checked' => 'checked')); - $expected = array( - 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'), - array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')) - ); - $this->assertTags($result, $expected); - - $result = $this->Form->checkbox('Model.field', array('checked' => 1)); - $expected = array( - 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'), - array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')) - ); - $this->assertTags($result, $expected); - - $result = $this->Form->checkbox('Model.field', array('checked' => true)); - $expected = array( - 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'), - array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')) - ); - $this->assertTags($result, $expected); - $this->Form->validationErrors['Model']['field'] = 1; $this->Form->data['Contact']['published'] = 1; $result = $this->Form->checkbox('Contact.published', array('id' => 'theID')); @@ -3834,6 +3813,49 @@ function testCheckbox() { $this->assertTags($result, $expected); } +/** + * test the checked option for checkboxes. + * + * @return void + */ + function testCheckboxCheckedOption() { + $result = $this->Form->checkbox('Model.field', array('checked' => 'checked')); + $expected = array( + 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'), + array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')) + ); + $this->assertTags($result, $expected); + + $result = $this->Form->checkbox('Model.field', array('checked' => 1)); + $expected = array( + 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'), + array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')) + ); + $this->assertTags($result, $expected); + + $result = $this->Form->checkbox('Model.field', array('checked' => true)); + $expected = array( + 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'), + array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')) + ); + $this->assertTags($result, $expected); + + $result = $this->Form->checkbox('Model.field', array('checked' => false)); + $expected = array( + 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'), + array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')) + ); + $this->assertTags($result, $expected); + + $this->Form->data['Model']['field'] = 1; + $result = $this->Form->checkbox('Model.field', array('checked' => false)); + $expected = array( + 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'), + array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')) + ); + $this->assertTags($result, $expected); + } + /** * Test that disabling a checkbox also disables the hidden input so no value is submitted *