Skip to content

Commit

Permalink
fix: set_radio() implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed May 27, 2022
1 parent f0465fa commit 4e5b173
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions system/Helpers/form_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"';
}
Expand All @@ -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"' : '';
}
}

Expand Down

0 comments on commit 4e5b173

Please sign in to comment.