From 38e85ebd6e41f258900294676ca981d70fd249c5 Mon Sep 17 00:00:00 2001 From: hiromi2424 Date: Thu, 10 Jun 2010 00:03:23 +0900 Subject: [PATCH] Supported default ion for FormHelper::checkbox(). Test added fixes #805 Signed-off-by: mark_story --- cake/libs/view/helpers/form.php | 13 +++++++--- .../cases/libs/view/helpers/form.test.php | 26 +++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/cake/libs/view/helpers/form.php b/cake/libs/view/helpers/form.php index 581746bdb..4ca29c340 100644 --- a/cake/libs/view/helpers/form.php +++ b/cake/libs/view/helpers/form.php @@ -990,14 +990,21 @@ class FormHelper extends AppHelper { * @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'; diff --git a/cake/tests/cases/libs/view/helpers/form.test.php b/cake/tests/cases/libs/view/helpers/form.test.php index 54d50e93b..5ff9ae980 100644 --- a/cake/tests/cases/libs/view/helpers/form.test.php +++ b/cake/tests/cases/libs/view/helpers/form.test.php @@ -2635,6 +2635,32 @@ class FormHelperTest extends CakeTestCase { $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 *