带参数的 CodeIgniter 回调函数
(编辑:我已经意识到也许不可能在回调中将数组作为参数传递?)
我正在尝试弄清楚如何将参数传递给 CI 中的回调函数。我已阅读文档,但除了以下内容之外,关于该主题的内容不多:
要调用回调,只需将函数名称放入规则中,并使用 “callback_”作为规则前缀。如果您需要额外获得 回调函数中的参数,只需在后面正常添加即可 方括号之间的函数名称,如:“callback_foo[bar]”, 那么它将作为回调的第二个参数传递 功能。
我想做的是创建一个回调来检查是否已选择了不应该选择的选项。因此,如果有人选择“请选择”选项,它将不会被添加到数据库中。任务类型只是一个包含主键和名称字段以及大约 10 行的表。
控制器 这是我的控制器代码(删减):
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..
}
}
回调 我的回调用于检查某些内容是否不可选择:
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;
}
查看 从模型返回的数据正常,但不存在验证错误。我的观点如下:
<? $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'); ?>
错误 我收到的错误是:
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.)
该错误表明传递的信息不是数组,但我什至将数组定义为默认值。我在回调函数中的 $options 数组上执行了 print_r() ,但它只是打印出了一个空数组。
谢谢。
(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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题就在这里:
这转换为字符串:
这就是当您在 PHP 中将数组视为字符串时会发生的情况。
这个问题是 Codeigniter 表单验证库的限制,没有正确的方法在回调或验证规则中使用数组,您必须使用字符串。试试这个:
这将产生类似
1|4|18|33
的内容。然后按照您当前的操作设置规则,但在回调中准备好管道分隔字符串而不是数组,并使用explode()
创建一个规则:The problem is here:
This translates to the string:
This is what happens when you treat arrays as strings in PHP.
This problem is a limitation of Codeigniter's form validation library, there is no proper way to use arrays in callbacks or validation rules, you'll have to use strings. Try this:
This will make something like
1|4|18|33
. Then set your rules as you are currently doing, but in your callback be prepared for a pipe delimited string rather than an array, and useexplode()
to create one:您应该扩展 CI_Form_validation 库而不是尝试使用回调。
要检查选择框是否有效,只需将默认值添加到其值,然后检查即可。
MY_Form_Validation
|->;
You should extend the CI_Form_validation lib rather than trying to use callbacks.
To check if a selectbox is valid simple add default to its value then check for that.
MY_Form_Validation
|->