From adea104edbb6a703e117f3ab4bb2d7ed636fbbf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Lorenzo=20Rodr=C3=ADguez?= Date: Fri, 30 Oct 2009 01:04:41 -0430 Subject: [PATCH] Allowing output disabling of hidden fields in checkbox and radio input types. closes #6185 Signed-off-by: Mark Story --- cake/libs/view/helpers/form.php | 30 +++++++++------ .../cases/libs/view/helpers/form.test.php | 37 ++++++++++++++++++- 2 files changed, 54 insertions(+), 13 deletions(-) diff --git a/cake/libs/view/helpers/form.php b/cake/libs/view/helpers/form.php index 6c0065c0c..b08bde13b 100755 --- a/cake/libs/view/helpers/form.php +++ b/cake/libs/view/helpers/form.php @@ -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); diff --git a/cake/tests/cases/libs/view/helpers/form.test.php b/cake/tests/cases/libs/view/helpers/form.test.php index 4619084e0..02e1e8943 100644 --- a/cake/tests/cases/libs/view/helpers/form.test.php +++ b/cake/tests/cases/libs/view/helpers/form.test.php @@ -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
/', $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); + } } ?> \ No newline at end of file