Skip to content

Commit 6bb449a

Browse files
committed
fix: set_radio() implementation
1 parent 8fa85dd commit 6bb449a

1 file changed

Lines changed: 14 additions & 12 deletions

File tree

system/Helpers/form_helper.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -650,14 +650,21 @@ function set_radio(string $field, string $value = '', bool $default = false): st
650650
$request = Services::request();
651651

652652
// Try any old input data we may have first
653-
$input = $request->getOldInput($field);
654-
if ($input === null) {
655-
$input = $request->getPost($field) ?? $default;
653+
$oldInput = $request->getOldInput($field);
654+
655+
$postInput = $request->getPost($field);
656+
657+
if ($oldInput !== null) {
658+
$input = $oldInput;
659+
} elseif ($postInput !== null) {
660+
$input = $postInput;
661+
} else {
662+
$input = $default;
656663
}
657664

658665
if (is_array($input)) {
659666
// Note: in_array('', array(0)) returns TRUE, do not use it
660-
foreach ($input as &$v) {
667+
foreach ($input as $v) {
661668
if ($value === $v) {
662669
return ' checked="checked"';
663670
}
@@ -667,16 +674,11 @@ function set_radio(string $field, string $value = '', bool $default = false): st
667674
}
668675

669676
// Unchecked checkbox and radio inputs are not even submitted by browsers ...
670-
$result = '';
671-
if ((string) $input === '0' || ! empty($input = $request->getPost($field)) || ! empty($input = old($field))) {
672-
$result = ($input === $value) ? ' checked="checked"' : '';
673-
}
674-
675-
if (empty($result)) {
676-
$result = ($default === true) ? ' checked="checked"' : '';
677+
if ($oldInput !== null || $postInput !== null) {
678+
return ((string) $input === $value) ? ' checked="checked"' : '';
677679
}
678680

679-
return $result;
681+
return ($default === true) ? ' checked="checked"' : '';
680682
}
681683
}
682684

0 commit comments

Comments
 (0)