downblast 发表于 2008-12-17 14:04:20

CI验证类回调函数的问题

$rules= array(   
    'task_end_time'   => 'trim|required|exact_length|callback_time_check',
);
$this->validation->set_rules($rules);
$this->validation->run();

function time_check($str) {
    return preg_match("/^()+$/", $str) ? true : false;
}
上面的验证毁掉函数为何总不成功?

[ 本帖最后由 downblast 于 2008-12-17 14:13 编辑 ]

Hex 发表于 2008-12-17 15:03:18

你是在控制器中,还是在模型中?如果是在模型中,原始的验证类无法实现。

downblast 发表于 2008-12-17 15:13:43

谢谢Hex的回答
model中无法调用验证类回调函数,最后通过扩展CI_validation类添加该函数搞定

alertger 发表于 2008-12-17 15:24:52

模型中不能使用回调函数的参考解决办法
1.回调函数写在调用该模型的控制器里
2.把模型的实例传给验证类;
Validation.php
185 行    增加参数
function run(& source = NULL)
285行 左右 更换如下代码
if ( ! method_exists($this->CI, $rule) )
                  {         
                        continue;
                  }
   $result = $this->CI->$rule($_POST[$field], $param);   

if ( method_exists($this->CI, $rule) ) {
                        $result = $this->CI->$rule($_POST[$field], $param);   
                  } else if ( !is_null($source) &&method_exists($source, $rule)) ) {
                        $result = $source->$rule($_POST[$field], $param);   
                  } else
                  {
                        continue;
                  }
使用时
$this->validation->run($this);

[ 本帖最后由 alertger 于 2008-12-17 18:13 编辑 ]

challenger 发表于 2009-1-3 18:50:50

可是我在控制器里为什么也不能调用呢?      function save() {
                $this->load->library('validation');

                $rules['username'] = "callback_username_check";
                $rules['password'] = "required|matches";
                $rules['passconf'] = "required";

                $fields['username'] = '用户名';
                $fields['password'] = '密码';
                $fields['passconf'] = '确认密码';

                $this->validation->set_rules($rules);
                $this->validation->set_fields($fields);
                $this->validation->set_error_delimiters('<div style="color:red">', '</div>');

                if ($this->validation->run() == false)
                {
                        $this->load->view('register');
                }
                else
                {
                        echo '注册成功';
                }
      }

      function username_check($username) {
                $this->validation->set_message('username_check', '%s 已被注册');
                return false;
      }

[ 本帖最后由 challenger 于 2009-1-3 18:52 编辑 ]

challenger 发表于 2009-1-3 18:55:37

不知道为什么改成这样可以了?

$rules['username'] = "required|callback_username_check";
页: [1]
查看完整版本: CI验证类回调函数的问题