Skip to content

Commit

Permalink
Supported default ion for FormHelper::checkbox(). Test added fixes #805
Browse files Browse the repository at this point in the history
Signed-off-by: mark_story <mark@mark-story.com>
  • Loading branch information
hiromi2424 authored and markstory committed Mar 16, 2011
1 parent 357588c commit 38e85eb
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
13 changes: 10 additions & 3 deletions cake/libs/view/helpers/form.php
Expand Up @@ -990,14 +990,21 @@ protected function _inputLabel($fieldName, $label, $options) {
* @link http://book.cakephp.org/view/1414/checkbox
*/
public function checkbox($fieldName, $options = array()) {
$valueOptions = array();
if(isset($options['default'])){
$valueOptions['default'] = $options['default'];
unset($options['default']);
}

$options = $this->_initInputField($fieldName, $options) + array('hiddenField' => true);
$value = current($this->value());
$value = current($this->value($valueOptions));
$output = "";

if (empty($options['value'])) {
$options['value'] = 1;
} elseif (
(!isset($options['checked']) && !empty($value) && $value === $options['value']) ||
}
if (
(!isset($options['checked']) && !empty($value) && $value == $options['value']) ||
!empty($options['checked'])
) {
$options['checked'] = 'checked';
Expand Down
26 changes: 26 additions & 0 deletions cake/tests/cases/libs/view/helpers/form.test.php
Expand Up @@ -2635,6 +2635,32 @@ function testDefaultValue() {
$this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
}

/**
* testCheckboxDefaultValue method
*
* Test default value setting on checkbox() method
*
* @access public
* @return void
*/
function testCheckboxDefaultValue() {
$this->Form->request->data['Model']['field'] = false;
$result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
$this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));

unset($this->Form->request->data['Model']['field']);
$result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
$this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));

$this->Form->request->data['Model']['field'] = true;
$result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
$this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));

unset($this->Form->request->data['Model']['field']);
$result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
$this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
}

/**
* testError method
*
Expand Down

0 comments on commit 38e85eb

Please sign in to comment.