Skip to content

Commit 38e85eb

Browse files
hiromi2424markstory
authored andcommitted
Supported default ion for FormHelper::checkbox(). Test added fixes #805
Signed-off-by: mark_story <mark@mark-story.com>
1 parent 357588c commit 38e85eb

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

cake/libs/view/helpers/form.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -990,14 +990,21 @@ protected function _inputLabel($fieldName, $label, $options) {
990990
* @link http://book.cakephp.org/view/1414/checkbox
991991
*/
992992
public function checkbox($fieldName, $options = array()) {
993+
$valueOptions = array();
994+
if(isset($options['default'])){
995+
$valueOptions['default'] = $options['default'];
996+
unset($options['default']);
997+
}
998+
993999
$options = $this->_initInputField($fieldName, $options) + array('hiddenField' => true);
994-
$value = current($this->value());
1000+
$value = current($this->value($valueOptions));
9951001
$output = "";
9961002

9971003
if (empty($options['value'])) {
9981004
$options['value'] = 1;
999-
} elseif (
1000-
(!isset($options['checked']) && !empty($value) && $value === $options['value']) ||
1005+
}
1006+
if (
1007+
(!isset($options['checked']) && !empty($value) && $value == $options['value']) ||
10011008
!empty($options['checked'])
10021009
) {
10031010
$options['checked'] = 'checked';

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2635,6 +2635,32 @@ function testDefaultValue() {
26352635
$this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
26362636
}
26372637

2638+
/**
2639+
* testCheckboxDefaultValue method
2640+
*
2641+
* Test default value setting on checkbox() method
2642+
*
2643+
* @access public
2644+
* @return void
2645+
*/
2646+
function testCheckboxDefaultValue() {
2647+
$this->Form->request->data['Model']['field'] = false;
2648+
$result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
2649+
$this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
2650+
2651+
unset($this->Form->request->data['Model']['field']);
2652+
$result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
2653+
$this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
2654+
2655+
$this->Form->request->data['Model']['field'] = true;
2656+
$result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
2657+
$this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
2658+
2659+
unset($this->Form->request->data['Model']['field']);
2660+
$result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
2661+
$this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
2662+
}
2663+
26382664
/**
26392665
* testError method
26402666
*

0 commit comments

Comments
 (0)