From 6bb449a4ad7a96c96c8b8a8d875643ad90c370bf Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 27 May 2022 13:48:17 +0900 Subject: [PATCH] fix: set_radio() implementation --- system/Helpers/form_helper.php | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/system/Helpers/form_helper.php b/system/Helpers/form_helper.php index 5e8ee6eebb82..2466900d0d3e 100644 --- a/system/Helpers/form_helper.php +++ b/system/Helpers/form_helper.php @@ -650,14 +650,21 @@ function set_radio(string $field, string $value = '', bool $default = false): st $request = Services::request(); // Try any old input data we may have first - $input = $request->getOldInput($field); - if ($input === null) { - $input = $request->getPost($field) ?? $default; + $oldInput = $request->getOldInput($field); + + $postInput = $request->getPost($field); + + if ($oldInput !== null) { + $input = $oldInput; + } elseif ($postInput !== null) { + $input = $postInput; + } else { + $input = $default; } if (is_array($input)) { // Note: in_array('', array(0)) returns TRUE, do not use it - foreach ($input as &$v) { + foreach ($input as $v) { if ($value === $v) { return ' checked="checked"'; } @@ -667,16 +674,11 @@ function set_radio(string $field, string $value = '', bool $default = false): st } // Unchecked checkbox and radio inputs are not even submitted by browsers ... - $result = ''; - if ((string) $input === '0' || ! empty($input = $request->getPost($field)) || ! empty($input = old($field))) { - $result = ($input === $value) ? ' checked="checked"' : ''; - } - - if (empty($result)) { - $result = ($default === true) ? ' checked="checked"' : ''; + if ($oldInput !== null || $postInput !== null) { + return ((string) $input === $value) ? ' checked="checked"' : ''; } - return $result; + return ($default === true) ? ' checked="checked"' : ''; } }