From 3f21d09c1d91ddfbddce6608ecf89811a412d4eb Mon Sep 17 00:00:00 2001 From: mark_story Date: Mon, 7 Jan 2013 21:12:53 -0500 Subject: [PATCH] Fix radio() and boolean values. Boolean values should be cast to integer equivalents, which allows for correct handling of boolean columns and their string equivalents used in form options. Fixes #3512 --- .../Test/Case/View/Helper/FormHelperTest.php | 30 ++++++++++++++++++- lib/Cake/View/Helper/FormHelper.php | 4 +++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php index 2dfe3c6a995..ae5b8fc9155 100644 --- a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php +++ b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php @@ -3407,10 +3407,11 @@ public function testRadio() { /** * Test that radios with a 0 value are selected under the correct conditions. + * Also ensure that values that are booleanish are handled correctly. * * @return void */ - public function testRadioOptionWithZeroValue() { + public function testRadioOptionWithBooleanishValues() { $expected = array( 'fieldset' => array(), 'legend' => array(), @@ -3432,6 +3433,9 @@ public function testRadioOptionWithZeroValue() { $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0)); $this->assertTags($result, $expected); + $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => false)); + $this->assertTags($result, $expected); + $expected = array( 'fieldset' => array(), 'legend' => array(), @@ -3456,6 +3460,30 @@ public function testRadioOptionWithZeroValue() { $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No')); $this->assertTags($result, $expected); + + $expected = array( + 'fieldset' => array(), + 'legend' => array(), + 'Field', + '/legend', + array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelField1')), + array('label' => array('for' => 'ModelField1')), + 'Yes', + '/label', + array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')), + array('label' => array('for' => 'ModelField0')), + 'No', + '/label', + '/fieldset' + ); + $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 1)); + $this->assertTags($result, $expected); + + $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1')); + $this->assertTags($result, $expected); + + $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => true)); + $this->assertTags($result, $expected); } /** diff --git a/lib/Cake/View/Helper/FormHelper.php b/lib/Cake/View/Helper/FormHelper.php index a3374f58dc7..b7961142f3a 100644 --- a/lib/Cake/View/Helper/FormHelper.php +++ b/lib/Cake/View/Helper/FormHelper.php @@ -1385,6 +1385,10 @@ public function radio($fieldName, $options = array(), $attributes = array()) { $hiddenField = isset($attributes['hiddenField']) ? $attributes['hiddenField'] : true; unset($attributes['hiddenField']); + if (isset($value) && is_bool($value)) { + $value = $value ? 1 : 0; + } + foreach ($options as $optValue => $optTitle) { $optionsHere = array('value' => $optValue);