mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 09:06:17 +00:00
Allowing output disabling of hidden fields in checkbox and radio input types. closes #6185
Signed-off-by: Mark Story <mark@mark-story.com>
This commit is contained in:
parent
1c47b2182e
commit
adea104edb
2 changed files with 54 additions and 13 deletions
|
@ -899,12 +899,14 @@ class FormHelper extends AppHelper {
|
|||
function checkbox($fieldName, $options = array()) {
|
||||
$options = $this->_initInputField($fieldName, $options);
|
||||
$value = current($this->value());
|
||||
$output = "";
|
||||
|
||||
if (!isset($options['value']) || empty($options['value'])) {
|
||||
$options['value'] = 1;
|
||||
} elseif (!empty($value) && $value === $options['value']) {
|
||||
$options['checked'] = 'checked';
|
||||
}
|
||||
if (!isset($options['hiddenField']) || $options['hiddenField'] != false) {
|
||||
$hiddenOptions = array(
|
||||
'id' => $options['id'] . '_', 'name' => $options['name'],
|
||||
'value' => '0', 'secure' => false
|
||||
|
@ -913,6 +915,8 @@ class FormHelper extends AppHelper {
|
|||
$hiddenOptions['disabled'] = 'disabled';
|
||||
}
|
||||
$output = $this->hidden($fieldName, $hiddenOptions);
|
||||
}
|
||||
unset($options['hiddenField']);
|
||||
|
||||
return $this->output($output . sprintf(
|
||||
$this->Html->tags['checkbox'],
|
||||
|
@ -972,6 +976,8 @@ class FormHelper extends AppHelper {
|
|||
if (isset($value) && $optValue == $value) {
|
||||
$optionsHere['checked'] = 'checked';
|
||||
}
|
||||
$hiddenField = isset($attributes['hiddenField']) ? $attributes['hiddenField'] : true;
|
||||
unset($attributes['hiddenField']);
|
||||
$parsedOptions = $this->_parseAttributes(
|
||||
array_merge($attributes, $optionsHere),
|
||||
array('name', 'type', 'id'), '', ' '
|
||||
|
@ -990,11 +996,13 @@ class FormHelper extends AppHelper {
|
|||
}
|
||||
$hidden = null;
|
||||
|
||||
if ($hiddenField) {
|
||||
if (!isset($value) || $value === '') {
|
||||
$hidden = $this->hidden($fieldName, array(
|
||||
'id' => $attributes['id'] . '_', 'value' => '', 'name' => $attributes['name']
|
||||
));
|
||||
}
|
||||
}
|
||||
$out = $hidden . join($inbetween, $out);
|
||||
|
||||
if ($legend) {
|
||||
|
|
|
@ -3296,7 +3296,7 @@ class FormHelperTest extends CakeTestCase {
|
|||
array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
|
||||
array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
|
||||
);
|
||||
$this->assertTags($result, $expected);
|
||||
$this->assertTags($result, $expected,true);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -5653,5 +5653,38 @@ class FormHelperTest extends CakeTestCase {
|
|||
);
|
||||
$this->assertTags($result, $expected);
|
||||
}
|
||||
|
||||
function testDisableHiddenField() {
|
||||
$result = $this->Form->input('UserForm.something', array(
|
||||
'type' => 'checkbox', 'hiddenField' => false
|
||||
)
|
||||
);
|
||||
$expected = array(
|
||||
'div' => array('class' => 'input checkbox'),
|
||||
array('input' => array(
|
||||
'type' => 'checkbox', 'name' => 'data[UserForm][something]',
|
||||
'value' => '1', 'id' => 'UserFormSomething'
|
||||
)),
|
||||
'label' => array('for' => 'UserFormSomething'),
|
||||
'Something',
|
||||
'/label',
|
||||
'/div'
|
||||
);
|
||||
$this->assertTags($result, $expected);
|
||||
|
||||
$result = $this->Form->input('Model.1.field', array(
|
||||
'type' => 'radio','options' => 'option A', 'hiddenField' => false
|
||||
)
|
||||
);
|
||||
$expected = array(
|
||||
'div' => array('class' => 'input radio'),
|
||||
'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
|
||||
'label' => array('for' => 'Model1Field0'),
|
||||
'option A',
|
||||
'/label',
|
||||
'/div'
|
||||
);
|
||||
$this->assertTags($result, $expected,true);
|
||||
}
|
||||
}
|
||||
?>
|
Loading…
Add table
Reference in a new issue