Skip to content

Commit b878058

Browse files
committed
Fixing issue where FormHelper::checkbox() would ignore an explicit checked = false, and use the post data instead. Test case added. Fixes #1437
1 parent 7548379 commit b878058

File tree

2 files changed

+47
-22
lines changed

2 files changed

+47
-22
lines changed

cake/libs/view/helpers/form.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1007,7 +1007,10 @@ function checkbox($fieldName, $options = array()) {
10071007

10081008
if (empty($options['value'])) {
10091009
$options['value'] = 1;
1010-
} elseif (!empty($value) && $value === $options['value']) {
1010+
} elseif (
1011+
(!isset($options['checked']) && !empty($value) && $value === $options['value']) ||
1012+
!empty($options['checked'])
1013+
) {
10111014
$options['checked'] = 'checked';
10121015
}
10131016
if ($options['hiddenField']) {

cake/tests/cases/libs/view/helpers/form.test.php

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3773,27 +3773,6 @@ function testCheckbox() {
37733773
);
37743774
$this->assertTags($result, $expected);
37753775

3776-
$result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
3777-
$expected = array(
3778-
'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
3779-
array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
3780-
);
3781-
$this->assertTags($result, $expected);
3782-
3783-
$result = $this->Form->checkbox('Model.field', array('checked' => 1));
3784-
$expected = array(
3785-
'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
3786-
array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
3787-
);
3788-
$this->assertTags($result, $expected);
3789-
3790-
$result = $this->Form->checkbox('Model.field', array('checked' => true));
3791-
$expected = array(
3792-
'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
3793-
array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
3794-
);
3795-
$this->assertTags($result, $expected);
3796-
37973776
$this->Form->validationErrors['Model']['field'] = 1;
37983777
$this->Form->data['Contact']['published'] = 1;
37993778
$result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
@@ -3834,6 +3813,49 @@ function testCheckbox() {
38343813
$this->assertTags($result, $expected);
38353814
}
38363815

3816+
/**
3817+
* test the checked option for checkboxes.
3818+
*
3819+
* @return void
3820+
*/
3821+
function testCheckboxCheckedOption() {
3822+
$result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
3823+
$expected = array(
3824+
'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
3825+
array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
3826+
);
3827+
$this->assertTags($result, $expected);
3828+
3829+
$result = $this->Form->checkbox('Model.field', array('checked' => 1));
3830+
$expected = array(
3831+
'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
3832+
array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
3833+
);
3834+
$this->assertTags($result, $expected);
3835+
3836+
$result = $this->Form->checkbox('Model.field', array('checked' => true));
3837+
$expected = array(
3838+
'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
3839+
array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
3840+
);
3841+
$this->assertTags($result, $expected);
3842+
3843+
$result = $this->Form->checkbox('Model.field', array('checked' => false));
3844+
$expected = array(
3845+
'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
3846+
array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
3847+
);
3848+
$this->assertTags($result, $expected);
3849+
3850+
$this->Form->data['Model']['field'] = 1;
3851+
$result = $this->Form->checkbox('Model.field', array('checked' => false));
3852+
$expected = array(
3853+
'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
3854+
array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
3855+
);
3856+
$this->assertTags($result, $expected);
3857+
}
3858+
38373859
/**
38383860
* Test that disabling a checkbox also disables the hidden input so no value is submitted
38393861
*

0 commit comments

Comments
 (0)