Skip to content

Commit e80b66f

Browse files
committed
Merge remote-tracking branch 'upstream/develop' into 4.4
2 parents f66ccb6 + 411a9bb commit e80b66f

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
@@ -866,6 +866,29 @@ public function testSetCheckboxWithValueZero(): void
866866
$this->assertSame(' checked="checked"', set_checkbox('foo', '0', true));
867867
}
868868

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

0 commit comments

Comments
 (0)