From 5225fe2b5c8cf1f24b044998aeff7e32ace62793 Mon Sep 17 00:00:00 2001 From: Kyle Robinson Young Date: Sun, 18 Dec 2011 23:34:19 -0800 Subject: [PATCH] Ability to set hiddenField value with FormHelper::checkbox Fixes #1811 --- .../Test/Case/View/Helper/FormHelperTest.php | 34 +++++++++++++++---- lib/Cake/View/Helper/FormHelper.php | 6 ++-- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php index 74bfaafb2..953ede1c7 100644 --- a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php +++ b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php @@ -4512,16 +4512,16 @@ class FormHelperTest extends CakeTestCase { } /** - * 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 @@ class FormHelperTest extends CakeTestCase { '/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); } /** diff --git a/lib/Cake/View/Helper/FormHelper.php b/lib/Cake/View/Helper/FormHelper.php index aa1e4896e..7350545b3 100644 --- a/lib/Cake/View/Helper/FormHelper.php +++ b/lib/Cake/View/Helper/FormHelper.php @@ -1232,8 +1232,10 @@ class FormHelper extends AppHelper { } 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';