更新
我正在创建一个codeigniter回调,用于验证用户在其中输入编程标记(例如php, js, jquery
)的输入。值用逗号分隔。
如果您输入重复的标记,例如php, jquery, php, js
,其中php
将是重复的,我想显示一条消息。
首先,在我的控制器中,我设置了"user_tags"输入的验证规则
$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'); ?>
当我输入重复标签时,我得到
Unable to access an error message corresponding to your field name.
我不确定我做错了什么。我在没有验证规则的静态页面中测试了该函数,它确实有效。
在user_tags_dublicates()
函数中设置user_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
真的能正确地返回标签吗?