Skip to content

Commit

Permalink
Merge pull request #2729 from musmanikram/2728-fox-radio-button-with-…
Browse files Browse the repository at this point in the history
…value-0

Fix issue set_radio when value is 0
  • Loading branch information
MGatner authored Mar 20, 2020
2 parents 84b9cc7 + 0927b74 commit 15f8fa4
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
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

0 comments on commit 15f8fa4

Please sign in to comment.