Codeigniter简单验证回调无效

时间:2011-05-29 20:32:55

标签: php function codeigniter

已更新

我正在创建一个codeigniter callback来验证用户输入编程标签的输入,例如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.

我不确定我做错了什么。我在没有验证规则的静态页面中测试了该函数,并且它可以工作。

2 个答案:

答案 0 :(得分:2)

user_tags功能

中设置user_tags_dublicates()的错误消息
   $this->form_validation->set_message('user_tags', 'The %s field can not have duplicate tags.');

答案 1 :(得分:1)

这可能听起来像是stoopid,但你检查过:

$tags = str_getcsv($val); //creates an array of the CSV

实际上是否正确返回了标签?

相关问题