Php Codeigniter简单验证回调不工作
更新 我正在创建一个用于验证用户输入编程标记(例如Php Codeigniter简单验证回调不工作,php,function,codeigniter,Php,Function,Codeigniter,更新 我正在创建一个用于验证用户输入编程标记(例如php、js、jquery)的输入。值之间用逗号分隔 如果您输入重复的标记,例如php、jquery、php、js,我想显示一条消息,其中php将是重复的 首先,在我的控制器中,我为“用户标签”输入设置验证规则 $this->form_validation->set_rules('user_tags', 'User Tags', 'callback_user_tags_dublicates', 'trim|xss_clean|max_
php、js、jquery
)的输入。值之间用逗号分隔
如果您输入重复的标记,例如php、jquery、php、js
,我想显示一条消息,其中php
将是重复的
首先,在我的控制器中,我为“用户标签”输入设置验证规则
$this->form_validation->set_rules('user_tags', 'User Tags', 'callback_user_tags_dublicates', 'trim|xss_clean|max_length[100]|regex_match[/^[a-z,0-9+# ]+$/i]');
然后是回调
<?php function user_tags_dublicates($str)
{
$val = $str; //the input value (all the CSV)
$tags = str_getcsv($val); //creates an array of the CSV
if(count($tags) != count(array_unique($tags))) //if array not equal to unique array it contains duplicates
{
$this->form_validation->set_message('user_tags', 'The %s field can not have duplicate tags.');
return FALSE;
}
else
{
return TRUE;
}
} ?>
最后在视图中,我显示了我的错误
<?php echo form_error('user_tags'); ?>
当我输入重复标记时,我得到
无法访问与字段名对应的错误消息。
我不确定我做错了什么。我在一个没有验证规则的静态页面中测试了这个函数,它可以正常工作 在
user\u tags\u dublicates()函数中为user\u tags
设置错误消息
$this->form_validation->set_message('user_tags', 'The %s field can not have duplicate tags.');
这听起来可能有点愚蠢,但您是否检查过:
$tags = str_getcsv($val); //creates an array of the CSV
是否正确返回标记?是,它会创建一个数组。我已经在没有验证规则的情况下测试了整个函数,它可以正常工作,因为我修复了,我得到了无法访问与您的字段名对应的错误消息。
nvm,我实际上必须在函数外部设置错误消息moveset\u message
时添加“dublicates”。例如,使用“”将其放置在您的set\u rules()
statementsTry下