CodeIgniter Forums
Fixing re-population of checkbox arrays - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Fixing re-population of checkbox arrays (/showthread.php?tid=5557)



Fixing re-population of checkbox arrays - El Forum - 01-26-2008

[eluser]marlar[/eluser]
Hi,

Being still new to CI it is sometimes difficult to know if I am doing something wrong or if I happened to find a bug. In this case I believe I found a bug in the validation library.

The problem is that checkboxes don't get re-populated if they are part of a group, like:
Code:
echo form_checkbox('group[]','family', $this->validation->set_checkbox('group', 'family'));
echo form_checkbox('group[]','friends', $this->validation->set_checkbox('group', 'friends'));

I changed set_checkbox() in the validation libary to:
Code:
function set_checkbox($field = '', $value = '')
    {
        if ($field == '' OR $value == '' OR  ! isset($_POST[$field]))
        {
            return '';
        }
            
        //Before: if ($_POST[$field] == $value)
        if ($_POST[$field] == $value || ( is_array($_POST[$field]) && in_array($value, $_POST[$field])) )
        {
            return ' checked="checked"';
        }
    }
and now it seems to work as expected.


Fixing re-population of checkbox arrays - El Forum - 01-26-2008

[eluser]Tom Glover[/eluser]
Could be a bug Undecided , post it in the bug forum and tracker and see what they say. Im still new to CI but i can normally see a bug a mile off Smile .


Fixing re-population of checkbox arrays - El Forum - 01-26-2008

[eluser]marlar[/eluser]
Good idea.

http://ellislab.com/forums/viewthread/69949/