Welcome Guest, Not a member yet? Register   Sign In
Population of checkbox arrays in Validation library
#1

[eluser]marlar[/eluser]
Hi,

I think I have found a bug in the validation library.

Checkboxes don't seem to 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'));

MODIFIED CODE; NOW IT ALSO WORKS FOR CHECKBOX GROUPS!
Here is a modification of set_checkbox() in the validation libary so it works as expected:
Code:
function set_checkbox($field = '', $value = '')
    {
        if ($field == '' OR $value == '')
        {
            return '';
        }
        if(isset($_POST[$field])) {
            if ($_POST[$field] == $value || ( is_array($_POST[$field]) && in_array($value, $_POST[$field])) )
            {
                return ' checked="checked"';
            }
        }
        elseif(isset($this->$field)) {
            if($this->$field==$value || ( is_array($this->$field) && array_search($value, $this->$field)!==FALSE) ) {
                return ' checked="checked"';
            }
        }
    }

HOW TO USE IT:

In the controller:
Code:
// for a single checkbox just use the checkbox' value:
$this->validation->mycheckbox = 1;
$this->validation->mycar = "big";

// for a checkbox group, use an array with the values that should be checked:
$this->validation->mycolors = array("red","blue","yellow");
These lines must come AFTER $this->validation->set_fields() !

In the view:
Code:
<input type="CHECKBOX" name="mycheckbox" value="1" <?=$this->validation->set_checkbox('mycheckbox', '1')?>>
<input type="CHECKBOX" name="mycar" value="big" <?=$this->validation->set_checkbox('mycar', 'big')?>>
<input type="CHECKBOX" name="mycolors[]" value="red" <?=$this->validation->set_checkbox('mycolors', 'red')?>>
<input type="CHECKBOX" name="mycolors[]" value="blue" <?=$this->validation->set_checkbox('mycolors', 'blue')?>>
<input type="CHECKBOX" name="mycolors[]" value="green" <?=$this->validation->set_checkbox('mycolors', 'green')?>>
<input type="CHECKBOX" name="mycolors[]" value="yellow" <?=$this->validation->set_checkbox('mycolors', 'yellow')?>>


Messages In This Thread
Population of checkbox arrays in Validation library - by El Forum - 01-26-2008, 10:43 AM
Population of checkbox arrays in Validation library - by El Forum - 02-07-2008, 01:11 PM
Population of checkbox arrays in Validation library - by El Forum - 02-07-2008, 02:24 PM
Population of checkbox arrays in Validation library - by El Forum - 04-16-2008, 10:08 PM
Population of checkbox arrays in Validation library - by El Forum - 04-17-2008, 09:39 AM



Theme © iAndrew 2016 - Forum software by © MyBB