Permalink
Browse files

Ability to set hiddenField value with FormHelper::checkbox

Fixes #1811
  • Loading branch information...
1 parent 2d68e7d commit 5225fe2b5c8cf1f24b044998aeff7e32ace62793 @shama shama committed Dec 19, 2011
Showing with 32 additions and 8 deletions.
  1. +28 −6 lib/Cake/Test/Case/View/Helper/FormHelperTest.php
  2. +4 −2 lib/Cake/View/Helper/FormHelper.php
View
34 lib/Cake/Test/Case/View/Helper/FormHelperTest.php
@@ -4512,16 +4512,16 @@ public function testCheckboxDisabling() {
}
/**
- * Test that the hidden input for checkboxes can be removed/omitted from the output.
+ * Test that the hidden input for checkboxes can be omitted or set to a
+ * specific value.
*
* @return void
*/
- public function testCheckboxHiddenFieldOmission() {
+ public function testCheckboxHiddenField() {
$result = $this->Form->input('UserForm.something', array(
- 'type' => 'checkbox',
- 'hiddenField' => false
- )
- );
+ 'type' => 'checkbox',
+ 'hiddenField' => false
+ ));
$expected = array(
'div' => array('class' => 'input checkbox'),
array('input' => array(
@@ -4534,6 +4534,28 @@ public function testCheckboxHiddenFieldOmission() {
'/div'
);
$this->assertTags($result, $expected);
+
+ $result = $this->Form->input('UserForm.something', array(
+ 'type' => 'checkbox',
+ 'value' => 'Y',
+ 'hiddenField' => 'N',
+ ));
+ $expected = array(
+ 'div' => array('class' => 'input checkbox'),
+ array('input' => array(
+ 'type' => 'hidden', 'name' => 'data[UserForm][something]',
+ 'value' => 'N', 'id' => 'UserFormSomething_'
+ )),
+ array('input' => array(
+ 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
+ 'value' => 'Y', 'id' => 'UserFormSomething'
+ )),
+ 'label' => array('for' => 'UserFormSomething'),
+ 'Something',
+ '/label',
+ '/div'
+ );
+ $this->assertTags($result, $expected);
}
/**
View
6 lib/Cake/View/Helper/FormHelper.php
@@ -1232,8 +1232,10 @@ public function checkbox($fieldName, $options = array()) {
}
if ($options['hiddenField']) {
$hiddenOptions = array(
- 'id' => $options['id'] . '_', 'name' => $options['name'],
- 'value' => '0', 'secure' => false
+ 'id' => $options['id'] . '_',
+ 'name' => $options['name'],
+ 'value' => ($options['hiddenField'] !== true ? $options['hiddenField'] : '0'),
+ 'secure' => false
);
if (isset($options['disabled']) && $options['disabled'] == true) {
$hiddenOptions['disabled'] = 'disabled';

0 comments on commit 5225fe2

Please sign in to comment.