Permalink
Browse files

Fixing issue where FormHelper::checkbox() would ignore an explicit ch…

…ecked = false, and use the post data instead. Test case added. Fixes #1437
  • Loading branch information...
1 parent 307a6fc commit ced8dcd6b6eacde6386c779a8fb803359e1302af @markstory markstory committed Jan 15, 2011
Showing with 47 additions and 8 deletions.
  1. +4 −1 cake/libs/view/helpers/form.php
  2. +43 −7 cake/tests/cases/libs/view/helpers/form.test.php
@@ -859,7 +859,10 @@ function checkbox($fieldName, $options = array()) {
if (!isset($options['value']) || 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';
}
$hiddenOptions = array(
@@ -3161,13 +3161,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);
-
$this->Form->validationErrors['Model']['field'] = 1;
$this->Form->data['Contact']['published'] = 1;
$result = $this->Form->checkbox('Contact.published', array('id'=>'theID'));
@@ -3208,6 +3201,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
*
* @return void

0 comments on commit ced8dcd

Please sign in to comment.