mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-18 18:46:17 +00:00
Supported default ion for FormHelper::checkbox(). Test added fixes #805
Signed-off-by: mark_story <mark@mark-story.com>
This commit is contained in:
parent
357588c60e
commit
38e85ebd6e
2 changed files with 36 additions and 3 deletions
|
@ -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';
|
||||
|
|
|
@ -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
|
||||
*
|
||||
|
|
Loading…
Add table
Reference in a new issue