Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: set_radio() not working as expected #6037

Merged
merged 7 commits into from
May 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 deletions system/Helpers/form_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,6 @@ function set_value(string $field, $default = '', bool $htmlEscape = true)
* Set Select
*
* Let's you set the selected value of a <select> menu via data in the POST array.
* If Form Validation is active it retrieves the info from the validation class
*/
function set_select(string $field, string $value = '', bool $default = false): string
{
Expand Down Expand Up @@ -608,7 +607,6 @@ function set_select(string $field, string $value = '', bool $default = false): s
* Set Checkbox
*
* Let's you set the selected value of a checkbox via the value in the POST array.
* If Form Validation is active it retrieves the info from the validation class
*/
function set_checkbox(string $field, string $value = '', bool $default = false): string
{
Expand Down Expand Up @@ -646,21 +644,27 @@ function set_checkbox(string $field, string $value = '', bool $default = false):
* Set Radio
*
* Let's you set the selected value of a radio field via info in the POST array.
* If Form Validation is active it retrieves the info from the validation class
*/
function set_radio(string $field, string $value = '', bool $default = false): string
{
$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 @@ -670,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
23 changes: 16 additions & 7 deletions tests/system/Helpers/FormHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -838,18 +838,19 @@ public function testSetCheckboxWithValueZero()
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testSetRadio()
public function testSetRadioFromSessionOldInput()
{
$_SESSION = [
'_ci_old_input' => [
'post' => [
'foo' => 'bar',
'foo' => '<bar>',
],
],
];

$this->assertSame(' checked="checked"', set_radio('foo', 'bar'));
$this->assertSame(' checked="checked"', set_radio('foo', '<bar>'));
$this->assertSame('', set_radio('foo', 'baz'));

unset($_SESSION['_ci_old_input']);
}

Expand All @@ -860,9 +861,10 @@ public function testSetRadio()
public function testSetRadioFromPost()
{
$_POST['bar'] = 'baz';

$this->assertSame(' checked="checked"', set_radio('bar', 'baz'));
$this->assertSame('', set_radio('bar', 'boop'));
$this->assertSame(' checked="checked"', set_radio('bar', 'boop', true));
$this->assertSame('', set_radio('bar', 'boop', true));
}

/**
Expand All @@ -871,15 +873,17 @@ public function testSetRadioFromPost()
*/
public function testSetRadioFromPostWithValueZero()
{
$_POST['bar'] = 0;
$_POST['bar'] = '0';

$this->assertSame(' checked="checked"', set_radio('bar', '0'));
$this->assertSame('', set_radio('bar', 'boop'));

$_POST = [];

$this->assertSame(' checked="checked"', set_radio('bar', '0', true));
}

public function testSetRadioFromPostArray()
public function testSetRadioFromSessionOldInputPostArray()
{
$_SESSION = [
'_ci_old_input' => [
Expand All @@ -891,11 +895,12 @@ public function testSetRadioFromPostArray()
],
],
];

$this->assertSame(' checked="checked"', set_radio('bar', 'boop'));
$this->assertSame('', set_radio('bar', 'baz'));
}

public function testSetRadioFromPostArrayWithValueZero()
public function testSetRadioFromSessionOldInputPostArrayWithValueZero()
{
$_SESSION = [
'_ci_old_input' => [
Expand All @@ -907,12 +912,16 @@ public function testSetRadioFromPostArrayWithValueZero()
],
],
];

$this->assertSame(' checked="checked"', set_radio('bar', '0'));
$this->assertSame('', set_radio('bar', 'baz'));
}

public function testSetRadioDefault()
{
$_SESSION = [];
$_POST = [];

$this->assertSame(' checked="checked"', set_radio('code', 'alpha', true));
$this->assertSame('', set_radio('code', 'beta', false));
}
Expand Down
5 changes: 0 additions & 5 deletions user_guide_src/source/helpers/form_helper.rst
Original file line number Diff line number Diff line change
Expand Up @@ -510,8 +510,3 @@ The following functions are available:
<input type="radio" name="myradio" value="1" <?= set_radio('myradio', '1', true) ?> />
<input type="radio" name="myradio" value="2" <?= set_radio('myradio', '2') ?> />

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