diff --git a/system/Helpers/form_helper.php b/system/Helpers/form_helper.php index 82c809ce3fa4..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"' : ''; } @@ -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"' : ''; } diff --git a/tests/system/Helpers/FormHelperTest.php b/tests/system/Helpers/FormHelperTest.php index 6df61f4ea2cf..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 @@ -755,6 +782,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 = [ @@ -771,6 +809,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));