Skip to content

Commit 45f8ba8

Browse files
authored
Merge pull request #6037 from kenjis/fix-set-radio
fix: `set_radio()` not working as expected
2 parents 6eb9afb + 6bb449a commit 45f8ba8

3 files changed

Lines changed: 30 additions & 27 deletions

File tree

system/Helpers/form_helper.php

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,6 @@ function set_value(string $field, $default = '', bool $htmlEscape = true)
571571
* Set Select
572572
*
573573
* Let's you set the selected value of a <select> menu via data in the POST array.
574-
* If Form Validation is active it retrieves the info from the validation class
575574
*/
576575
function set_select(string $field, string $value = '', bool $default = false): string
577576
{
@@ -608,7 +607,6 @@ function set_select(string $field, string $value = '', bool $default = false): s
608607
* Set Checkbox
609608
*
610609
* Let's you set the selected value of a checkbox via the value in the POST array.
611-
* If Form Validation is active it retrieves the info from the validation class
612610
*/
613611
function set_checkbox(string $field, string $value = '', bool $default = false): string
614612
{
@@ -646,21 +644,27 @@ function set_checkbox(string $field, string $value = '', bool $default = false):
646644
* Set Radio
647645
*
648646
* Let's you set the selected value of a radio field via info in the POST array.
649-
* If Form Validation is active it retrieves the info from the validation class
650647
*/
651648
function set_radio(string $field, string $value = '', bool $default = false): string
652649
{
653650
$request = Services::request();
654651

655652
// Try any old input data we may have first
656-
$input = $request->getOldInput($field);
657-
if ($input === null) {
658-
$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;
659663
}
660664

661665
if (is_array($input)) {
662666
// Note: in_array('', array(0)) returns TRUE, do not use it
663-
foreach ($input as &$v) {
667+
foreach ($input as $v) {
664668
if ($value === $v) {
665669
return ' checked="checked"';
666670
}
@@ -670,16 +674,11 @@ function set_radio(string $field, string $value = '', bool $default = false): st
670674
}
671675

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

682-
return $result;
681+
return ($default === true) ? ' checked="checked"' : '';
683682
}
684683
}
685684

tests/system/Helpers/FormHelperTest.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -838,18 +838,19 @@ public function testSetCheckboxWithValueZero()
838838
* @runInSeparateProcess
839839
* @preserveGlobalState disabled
840840
*/
841-
public function testSetRadio()
841+
public function testSetRadioFromSessionOldInput()
842842
{
843843
$_SESSION = [
844844
'_ci_old_input' => [
845845
'post' => [
846-
'foo' => 'bar',
846+
'foo' => '<bar>',
847847
],
848848
],
849849
];
850850

851-
$this->assertSame(' checked="checked"', set_radio('foo', 'bar'));
851+
$this->assertSame(' checked="checked"', set_radio('foo', '<bar>'));
852852
$this->assertSame('', set_radio('foo', 'baz'));
853+
853854
unset($_SESSION['_ci_old_input']);
854855
}
855856

@@ -860,9 +861,10 @@ public function testSetRadio()
860861
public function testSetRadioFromPost()
861862
{
862863
$_POST['bar'] = 'baz';
864+
863865
$this->assertSame(' checked="checked"', set_radio('bar', 'baz'));
864866
$this->assertSame('', set_radio('bar', 'boop'));
865-
$this->assertSame(' checked="checked"', set_radio('bar', 'boop', true));
867+
$this->assertSame('', set_radio('bar', 'boop', true));
866868
}
867869

868870
/**
@@ -871,15 +873,17 @@ public function testSetRadioFromPost()
871873
*/
872874
public function testSetRadioFromPostWithValueZero()
873875
{
874-
$_POST['bar'] = 0;
876+
$_POST['bar'] = '0';
877+
875878
$this->assertSame(' checked="checked"', set_radio('bar', '0'));
876879
$this->assertSame('', set_radio('bar', 'boop'));
877880

878881
$_POST = [];
882+
879883
$this->assertSame(' checked="checked"', set_radio('bar', '0', true));
880884
}
881885

882-
public function testSetRadioFromPostArray()
886+
public function testSetRadioFromSessionOldInputPostArray()
883887
{
884888
$_SESSION = [
885889
'_ci_old_input' => [
@@ -891,11 +895,12 @@ public function testSetRadioFromPostArray()
891895
],
892896
],
893897
];
898+
894899
$this->assertSame(' checked="checked"', set_radio('bar', 'boop'));
895900
$this->assertSame('', set_radio('bar', 'baz'));
896901
}
897902

898-
public function testSetRadioFromPostArrayWithValueZero()
903+
public function testSetRadioFromSessionOldInputPostArrayWithValueZero()
899904
{
900905
$_SESSION = [
901906
'_ci_old_input' => [
@@ -907,12 +912,16 @@ public function testSetRadioFromPostArrayWithValueZero()
907912
],
908913
],
909914
];
915+
910916
$this->assertSame(' checked="checked"', set_radio('bar', '0'));
911917
$this->assertSame('', set_radio('bar', 'baz'));
912918
}
913919

914920
public function testSetRadioDefault()
915921
{
922+
$_SESSION = [];
923+
$_POST = [];
924+
916925
$this->assertSame(' checked="checked"', set_radio('code', 'alpha', true));
917926
$this->assertSame('', set_radio('code', 'beta', false));
918927
}

user_guide_src/source/helpers/form_helper.rst

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -510,8 +510,3 @@ The following functions are available:
510510
<input type="radio" name="myradio" value="1" <?= set_radio('myradio', '1', true) ?> />
511511
<input type="radio" name="myradio" value="2" <?= set_radio('myradio', '2') ?> />
512512

513-
.. note:: If you are using the Validation class, you must always specify
514-
a rule for your field, even if empty, in order for the ``set_*()``
515-
functions to work. This is because if a Validation object is
516-
defined, the control for ``set_*()`` is handed over to a method of the
517-
class instead of the generic helper function.

0 commit comments

Comments
 (0)