【问题标题】:validation max_length, min_length in codeigniter在 codeigniter 中验证 max_length、min_length
【发布时间】:2012-01-19 04:47:24
【问题描述】:

尝试在CodeIgniter 中为min_lengthmax_length 验证约束设置自定义验证消息时出现错误。

这些是我的验证规则:

$this->form_validation->set_rules('username', 'Username', 'required|xss_clean|min_length[6]|max_length[10]');
$this->form_validation->set_rules('password', 'Password', 'required|min_length[8]|max_length[20]';

这些是我的自定义验证消息:

$this->form_validation->set_message('min_length[3]', '%s: the minimum of characters is three');
$this->form_validation->set_message('max_length[20]', '%s:the maximum of characters is 20');

在我的示例中,我们有两个字段,但我有许多具有不同最小和最大长度值的字段。它们都有特定的约束验证。此消息不起作用。默认情况下会显示该消息。感谢您的帮助。

【问题讨论】:

    标签: php codeigniter validation


    【解决方案1】:

    只需添加另一个%s 即可获取长度值..

    $this->form_validation->set_message('min_length', '%s: the minimum of characters is %s');
    

    只有当您使用%s 第二次时,您才会给出大小。第一次它会给出你指定的字段名称..

    【讨论】:

      【解决方案2】:

      如果您想显示自己的验证消息,请执行以下操作:

      $this->form_validation->set_message('min_length','YOUR MESSAGE');
      

      没有分支:[num]

      问候

      【讨论】:

        【解决方案3】:

        使用set_message 时,您无法按照您的方式指定长度(最小值和最大值)。您可以为min_lengthmax_length 设置一般消息,例如:

        $this->form_validation->set_message('min_length', 'The value you have entered for %s is too short');
        $this->form_validation->set_message('max_length', 'The value you have entered for %s is too long');
        

        否则,您将不得不利用CodeIgniter 的回调功能并设置一个看起来像这样的自定义验证函数(对于最小长度!!)。

        public function custom_min_length($str, $val) {
            if (preg_match("/[^0-9]/", $val)) {
                return FALSE;
            }
            if (function_exists('mb_strlen')) {
                if(mb_strlen($str) < $val) {
                    $this->form_validation->set_message('custom_min_length', '%s: The Minimum Number Of Characters Is ' . $val);
                    return FALSE;
                } else {
                    return TRUE;
                }
            }
            if(strlen($str) < $val) {
                $this->form_validation->set_message('custom_min_length', '%s: The Minimum Number Of Characters Is ' . $val);
                return FALSE;
            } else {
                return TRUE;
            }
        }
        

        您可以这样设置验证规则:

        $this->form_validation->set_rules('username', 'Username', 'callback_custom_min_length[6]|required|xss_clean');
        

        注意custom_min_length 规则上的callback_ 前缀。这将替换并用于代替通常的 min_length CI 函数。

        希望对您有所帮助...我会让您弄清楚如何使用 custom_max_length 函数 :)

        【讨论】:

        • 感谢您的帮助,我会审核您的回答。
        【解决方案4】:
        $this->form_validation->set_rules('password', 'Password', 'required|min_length[8]|max_length[20]';
        

        缺少右括号 -- ):

        $this->form_validation->set_rules('password', 'Password', 'required|min_length[8]|max_length[20]');
        


        更新
        也许使用callback 函数会更好?例如:

        $this->form_validation->set_rules('username', 'Username', 'required|xss_clean|callback__check_length[6,10]');
        
        
        /**
         * Callback function. Checks if the length of the user's input
         * conforms to minimum and maximum character length requirements
         *
         * @param  string
         * @param  int
         * @param  int
         * @return bool
         */
        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;        
            }
        }
        

        【讨论】:

        • 在我的源代码中没问题。我发帖的时候忘记把结尾加括号了。我的怀疑还在继续。感谢您的指正。
        • 谢谢,我会查看您的回答。谢谢。
        猜你喜欢
        • 1970-01-01
        • 2011-03-13
        • 1970-01-01
        • 1970-01-01
        • 2012-05-21
        • 1970-01-01
        • 2010-12-31
        • 2011-06-22
        • 1970-01-01
        相关资源
        最近更新 更多