Supported default ion for FormHelper::checkbox(). Test added fixes #805

Signed-off-by: mark_story <mark@mark-story.com>
This commit is contained in:
hiromi2424 2010-06-10 00:03:23 +09:00 committed by mark_story
parent 357588c60e
commit 38e85ebd6e
2 changed files with 36 additions and 3 deletions

View file

@ -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';

View file

@ -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
*