(Edit: I've since realised that perhaps it's not possible to pass arrays as parameters in callbacks?)
I'm trying to work out how to pass parameters to a callback function in CI. I've read the documentation but there isn't much on the subject except:
To invoke a callback just put the function name in a rule, with "callback_" as the rule prefix. If you need to receive an extra parameter in your callback function, just add it normally after the function name between square brackets, as in: "callback_foo[bar]", then it will be passed as the second argument of your callback function.
What I'm trying to do is create a callback which checks if an has been selected which shouldn't have been. So if someone selects the option which says "Please select" it won't be added to the database. Task types is just a table with primary keys and a name field and about 10 rows.
Controller So here is my controller code (cut down):
function Add_Task()
{
$task_types_get = $this->task_model->Get_Task_Types();//Get available task types.
$this->options->task_types = $task_types_get->result();
$not_selectable = array(1);//Non selectable values for select. Added to callback function below for validation. These are pks.
$this->form_validation->set_rules("task_type","Task Types","required|callback__Not_Selectable[$not_selectable]");
if($this->form_validation->run())
{
//Add to db etc..
}
}
Callback And my callback to check if something is not selectable:
function _Not_Selectable($option,$values=array())
{
if(in_array($option,$values))//Is the value invalid?
{
$this->form_validation->set_message('_Not_Selectable', 'That option is not selectable.');
return false;
}
return true;
}
View The data which is coming back from the model is ok, but there are not validation errors. My view is as follows:
<? $this->load->view('includes/header'); ?>
<?=form_open();?>
<div class="form_row">
<div class="form_field">
<label for="task_desc">Task Type</label>
</div>
<div class="form_field name_element" id="name-list">
<select name="task_type" id="task_select">
<? foreach($task_types as $t => $v):
echo "<option ".set_select('task_type', $v->tt_id)." value=\"{$v->tt_id}\">{$v->name}</option>\n";
endforeach;
?>
</select>
<?=form_error('task_type');?>
</div>
</div>
<?=form_submit('add_task', 'Add Task');?>
<?=form_close();?>
<? $this->load->view('includes/footer'); ?>
Errors The error I'm getting is:
A PHP Error was encountered
Severity: Warning
Message: in_array() [function.in-array]: Wrong datatype for second argument
Filename: controllers/tasks.php
Line Number: 112 (NOTE: this is the in_array line in the callback function.)
The error suggests that the information passed is not an array but I've even defined array as default. I did a print_r() on the $options array in the callback function but it just printed out an empty Array.
Thanks.