[eluser]marlar[/eluser]
[quote author="wiredesignz" date="1201411812"]
to pre-load values into the validation object(after set_fields()):
Code:
//in your controller
$this->validation->mycheck = 1;
[/quote]
That is what I tried, but it doesn't work. The validation library is too limited.
However, I have modified it so it works now - also for checkbox arrays!
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"';
}
}
}
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");
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')?>>