CodeIgniter Forums
form_checkbox with set_checkbox - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: form_checkbox with set_checkbox (/showthread.php?tid=72235)



form_checkbox with set_checkbox - obozdag - 11-26-2018

Is it possible to use set_checkbox helper with form_checkbox function?


RE: form_checkbox with set_checkbox - IvanBell - 02-01-2021

This question was submitted a couple of years ago, but maybe my answer will benefit someone. I was working on a form and ended up with this solution:

Code:
$checkbox_data = [
  'name' => 'name',
  'value' => '1',
  'checked' => set_checkbox('name', '1') != '' ? true : false
];


Notice how "checked" is set. The set_checkbox method returns either ' checked="checked"' or an empty string. We can check it and set true or false. After that in view we can use this:

Code:
<?= form_checkbox(checkbox_data) ?>