我如何将第三个参数传递给表单验证回调函数,即字段名称?

时间:2018-04-30 20:23:08

标签: codeigniter validation

这是验证码:

public function registration () {
//
//
// -- code
      $this->form_validation->set_rules('password', 'Password', 'required|callback__check_length[6,10]');

}


   function _check_length($input, $min, $max)
    {
        $length = strlen($input);

        if ($length <= $max && $length >= $min)
        {
            return TRUE;
        }
        elseif ($length < $min)
        {
            $this->form_validation->set_message('_check_length', 'Minimum number of characters is ' . $min);
            return FALSE;
        }
        elseif ($length > $max)
        {
            $this->form_validation->set_message('_check_length', 'Maximum number of characters is ' . $max);
            return FALSE;
        }
    }

它给了我错误: 消息:缺少Person :: _ check_length()的参数3,在第744行的C:\ wamp64 \ www \ abc \ system \ libraries \ Form_validation.php中调用并定义

1 个答案:

答案 0 :(得分:1)

你可以通过爆炸第二个参数来实现。 CI似乎不支持函数args中的3个参数用于表单验证规则:

public function _check_length($input, $minmax) {

    $minmax = explode(',', $minmax);
    $min = $minmax[0];
    $max = $minmax[1];

    $length = strlen($input);

    if ($length <= $max && $length >= $min) {
        return TRUE;
    } elseif ($length < $min) {
        $this->form_validation->set_message('_check_length', 'Minimum number of characters is ' . $min);
        return FALSE;
    } elseif ($length > $max) {
        $this->form_validation->set_message('_check_length', 'Maximum number of characters is ' . $max);
        return FALSE;
    }
}

您也没有自己的功能来执行此操作。您只需使用min_length[x]max_length[x]规则。

https://www.codeigniter.com/userguide3/libraries/form_validation.html#rule-reference