Permalink
Browse files

Allowing output disabling of hidden fields in checkbox and radio inpu…

…t types. closes #6185

Signed-off-by: Mark Story <mark@mark-story.com>
  • Loading branch information...
1 parent 1c47b21 commit adea104edbb6a703e117f3ab4bb2d7ed636fbbf9 @lorenzo lorenzo committed with markstory Oct 30, 2009
Showing with 54 additions and 13 deletions.
  1. +19 −11 cake/libs/view/helpers/form.php
  2. +35 −2 cake/tests/cases/libs/view/helpers/form.test.php
@@ -899,20 +899,24 @@ function input($fieldName, $options = array()) {
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 @@ function radio($fieldName, $options = array(), $attributes = array()) {
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 @@ function radio($fieldName, $options = array(), $attributes = array()) {
}
$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);
@@ -3296,7 +3296,7 @@ function testCheckboxDisabling() {
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 @@ function testInputErrorEscape() {
$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 @@ function testFormEncoding() {
);
$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);
+ }
}
?>

0 comments on commit adea104

Please sign in to comment.