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:
José Lorenzo Rodríguez 2009-10-30 01:04:41 -04:30 committed by mark_story
parent 1c47b2182e
commit adea104edb
2 changed files with 54 additions and 13 deletions

View file

@ -899,20 +899,24 @@ 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';
}
$hiddenOptions = array(
'id' => $options['id'] . '_', 'name' => $options['name'],
'value' => '0', 'secure' => false
);
if (isset($options['disabled']) && $options['disabled'] == true) {
$hiddenOptions['disabled'] = 'disabled';
if (!isset($options['hiddenField']) || $options['hiddenField'] != false) {
$hiddenOptions = array(
'id' => $options['id'] . '_', 'name' => $options['name'],
'value' => '0', 'secure' => false
);
if (isset($options['disabled']) && $options['disabled'] == true) {
$hiddenOptions['disabled'] = 'disabled';
}
$output = $this->hidden($fieldName, $hiddenOptions);
}
$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,10 +996,12 @@ class FormHelper extends AppHelper {
}
$hidden = null;
if (!isset($value) || $value === '') {
$hidden = $this->hidden($fieldName, array(
'id' => $attributes['id'] . '_', 'value' => '', 'name' => $attributes['name']
));
if ($hiddenField) {
if (!isset($value) || $value === '') {
$hidden = $this->hidden($fieldName, array(
'id' => $attributes['id'] . '_', 'value' => '', 'name' => $attributes['name']
));
}
}
$out = $hidden . join($inbetween, $out);

View file

@ -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);
}
/**
@ -5636,7 +5636,7 @@ class FormHelperTest extends CakeTestCase {
$result = $this->Form->input('city',array('error' => array('escape' => false)));
$this->assertPattern('/required<br>/', $result);
}
function testFormEncoding() {
$result = $this->Form->create('UserForm', array(
'type' => 'post', 'action' => 'login','encoding' => 'iso-8859-1'
@ -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);
}
}
?>