From 034e3e6f2225b5b29ae5551e8af2b78327a1c8c3 Mon Sep 17 00:00:00 2001 From: Usman Ikram Date: Fri, 20 Mar 2020 16:45:16 +0100 Subject: [PATCH 1/5] Fix issue set_radio when value is 0 --- system/Helpers/form_helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Helpers/form_helper.php b/system/Helpers/form_helper.php index 82c809ce3fa4..ba7ef81082ff 100644 --- a/system/Helpers/form_helper.php +++ b/system/Helpers/form_helper.php @@ -891,7 +891,7 @@ function set_radio(string $field, string $value = '', bool $default = false): st // Unchecked checkbox and radio inputs are not even submitted by browsers ... $result = ''; - if (! empty($input = $request->getPost($field)) || ! empty($input = old($field))) + if (intval($input) === 0 || ! empty($input = $request->getPost($field)) || ! empty($input = old($field))) { $result = ($input === $value) ? ' checked="checked"' : ''; } From bdae3f9a7dc62a460833ebe9940d46b8a3fa3991 Mon Sep 17 00:00:00 2001 From: Usman Ikram Date: Fri, 20 Mar 2020 16:49:16 +0100 Subject: [PATCH 2/5] Add unit test for set_radio() with value 0 --- tests/system/Helpers/FormHelperTest.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/system/Helpers/FormHelperTest.php b/tests/system/Helpers/FormHelperTest.php index 6df61f4ea2cf..dd2ff51a5a0c 100644 --- a/tests/system/Helpers/FormHelperTest.php +++ b/tests/system/Helpers/FormHelperTest.php @@ -755,6 +755,17 @@ public function testSetRadioFromPost() $this->assertEquals('', set_radio('bar', 'boop')); } + /** + * @runInSeparateProcess + * @preserveGlobalState disabled + */ + public function testSetRadioFromPostWithValueZero() + { + $_POST['bar'] = 0; + $this->assertEquals(' checked="checked"', set_radio('bar', '0')); + $this->assertEquals('', set_radio('bar', 'boop')); + } + public function testSetRadioFromPostArray() { $_SESSION = [ From fa86c6cfcf43cd7394332a9366f946f21a807c4c Mon Sep 17 00:00:00 2001 From: Usman Ikram Date: Fri, 20 Mar 2020 17:20:16 +0100 Subject: [PATCH 3/5] Add unit test for array eith value 0 --- tests/system/Helpers/FormHelperTest.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/system/Helpers/FormHelperTest.php b/tests/system/Helpers/FormHelperTest.php index dd2ff51a5a0c..93377d77b9bd 100644 --- a/tests/system/Helpers/FormHelperTest.php +++ b/tests/system/Helpers/FormHelperTest.php @@ -782,6 +782,22 @@ public function testSetRadioFromPostArray() $this->assertEquals('', set_radio('bar', 'baz')); } + public function testSetRadioFromPostArrayWithValueZero() + { + $_SESSION = [ + '_ci_old_input' => [ + 'post' => [ + 'bar' => [ + '0', + 'fuzzy', + ], + ], + ], + ]; + $this->assertEquals(' checked="checked"', set_radio('bar', '0')); + $this->assertEquals('', set_radio('bar', 'baz')); + } + public function testSetRadioDefault() { $this->assertEquals(' checked="checked"', set_radio('code', 'alpha', true)); From 0095146138f25c690dfd7dbd250f39c34d2873c8 Mon Sep 17 00:00:00 2001 From: Usman Ikram Date: Fri, 20 Mar 2020 17:23:03 +0100 Subject: [PATCH 4/5] Fix 0 value issue for checkbox also --- system/Helpers/form_helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Helpers/form_helper.php b/system/Helpers/form_helper.php index ba7ef81082ff..7f39ebe03bc8 100644 --- a/system/Helpers/form_helper.php +++ b/system/Helpers/form_helper.php @@ -839,7 +839,7 @@ function set_checkbox(string $field, string $value = '', bool $default = false): } // Unchecked checkbox and radio inputs are not even submitted by browsers ... - if (! empty($request->getPost()) || ! empty(old($field))) + if (intval($input) === 0 || ! empty($request->getPost()) || ! empty(old($field))) { return ($input === $value) ? ' checked="checked"' : ''; } From 0927b7496270c9eea5de6bda469a1f514bdf51c2 Mon Sep 17 00:00:00 2001 From: Usman Ikram Date: Fri, 20 Mar 2020 17:35:37 +0100 Subject: [PATCH 5/5] Add test for zero value in set_checkbox --- tests/system/Helpers/FormHelperTest.php | 27 +++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/system/Helpers/FormHelperTest.php b/tests/system/Helpers/FormHelperTest.php index 93377d77b9bd..7d5c96ba1678 100644 --- a/tests/system/Helpers/FormHelperTest.php +++ b/tests/system/Helpers/FormHelperTest.php @@ -724,6 +724,33 @@ public function testSetCheckbox() $this->assertEquals('', set_checkbox('foo', 'bar')); } + // ------------------------------------------------------------------------ + public function testSetCheckboxWithValueZero() + { + $_SESSION = [ + '_ci_old_input' => [ + 'post' => [ + 'foo' => '0', + ], + ], + ]; + + $this->assertEquals(' checked="checked"', set_checkbox('foo', '0')); + + $_SESSION = [ + '_ci_old_input' => [ + 'post' => [ + 'foo' => ['foo' => '0'], + ], + ], + ]; + $this->assertEquals(' checked="checked"', set_checkbox('foo', '0')); + $this->assertEquals('', set_checkbox('foo', 'baz')); + + $_SESSION = []; + $this->assertEquals('', set_checkbox('foo', 'bar')); + } + // ------------------------------------------------------------------------ /** * @runInSeparateProcess