Skip to content

Commit 411a9bb

Browse files
authored
Merge pull request #7818 from kenjis/fix-set_checkbox
fix: `set_checkbox()` checks unchecked checkbox
2 parents cfd8898 + f2f7dcf commit 411a9bb

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

system/Helpers/form_helper.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,8 +629,11 @@ function set_checkbox(string $field, string $value = '', bool $default = false):
629629
return '';
630630
}
631631

632+
$session = Services::session();
633+
$hasOldInput = $session->has('_ci_old_input');
634+
632635
// Unchecked checkbox and radio inputs are not even submitted by browsers ...
633-
if ((string) $input === '0' || ! empty($request->getPost()) || ! empty(old($field))) {
636+
if ((string) $input === '0' || ! empty($request->getPost()) || $hasOldInput) {
634637
return ($input === $value) ? ' checked="checked"' : '';
635638
}
636639

tests/system/Helpers/FormHelperTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,29 @@ public function testSetCheckboxWithValueZero(): void
867867
$this->assertSame(' checked="checked"', set_checkbox('foo', '0', true));
868868
}
869869

870+
/**
871+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/7814
872+
*/
873+
public function testSetCheckboxWithUnchecked(): void
874+
{
875+
$_SESSION = [
876+
'_ci_old_input' => [
877+
'post' => [
878+
],
879+
],
880+
];
881+
882+
$this->assertSame(
883+
'',
884+
set_checkbox('fruit', 'apple', true)
885+
);
886+
887+
$this->assertSame(
888+
'',
889+
set_checkbox('fruit', 'apple')
890+
);
891+
}
892+
870893
/**
871894
* @runInSeparateProcess
872895
* @preserveGlobalState disabled

0 commit comments

Comments
 (0)