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 issue set_radio when value is 0 #2729

Merged
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
4 changes: 2 additions & 2 deletions system/Helpers/form_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,7 @@ function set_checkbox(string $field, string $value = '', bool $default = false):
}

// Unchecked checkbox and radio inputs are not even submitted by browsers ...
if (! empty($request->getPost()) || ! empty(old($field)))
if (intval($input) === 0 || ! empty($request->getPost()) || ! empty(old($field)))
{
return ($input === $value) ? ' checked="checked"' : '';
}
Expand Down Expand Up @@ -891,7 +891,7 @@ function set_radio(string $field, string $value = '', bool $default = false): st

// Unchecked checkbox and radio inputs are not even submitted by browsers ...
$result = '';
if (! empty($input = $request->getPost($field)) || ! empty($input = old($field)))
if (intval($input) === 0 || ! empty($input = $request->getPost($field)) || ! empty($input = old($field)))
{
$result = ($input === $value) ? ' checked="checked"' : '';
}
Expand Down
54 changes: 54 additions & 0 deletions tests/system/Helpers/FormHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,33 @@ public function testSetCheckbox()
$this->assertEquals('', set_checkbox('foo', 'bar'));
}

// ------------------------------------------------------------------------
public function testSetCheckboxWithValueZero()
{
$_SESSION = [
'_ci_old_input' => [
'post' => [
'foo' => '0',
],
],
];

$this->assertEquals(' checked="checked"', set_checkbox('foo', '0'));

$_SESSION = [
'_ci_old_input' => [
'post' => [
'foo' => ['foo' => '0'],
],
],
];
$this->assertEquals(' checked="checked"', set_checkbox('foo', '0'));
$this->assertEquals('', set_checkbox('foo', 'baz'));

$_SESSION = [];
$this->assertEquals('', set_checkbox('foo', 'bar'));
}

// ------------------------------------------------------------------------
/**
* @runInSeparateProcess
Expand Down Expand Up @@ -755,6 +782,17 @@ public function testSetRadioFromPost()
$this->assertEquals('', set_radio('bar', 'boop'));
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testSetRadioFromPostWithValueZero()
{
$_POST['bar'] = 0;
$this->assertEquals(' checked="checked"', set_radio('bar', '0'));
$this->assertEquals('', set_radio('bar', 'boop'));
}

public function testSetRadioFromPostArray()
{
$_SESSION = [
Expand All @@ -771,6 +809,22 @@ public function testSetRadioFromPostArray()
$this->assertEquals('', set_radio('bar', 'baz'));
}

public function testSetRadioFromPostArrayWithValueZero()
{
$_SESSION = [
'_ci_old_input' => [
'post' => [
'bar' => [
'0',
'fuzzy',
],
],
],
];
$this->assertEquals(' checked="checked"', set_radio('bar', '0'));
$this->assertEquals('', set_radio('bar', 'baz'));
}

public function testSetRadioDefault()
{
$this->assertEquals(' checked="checked"', set_radio('code', 'alpha', true));
Expand Down