php codeigniter 中的验证 max_length、min_length

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8487066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:50:53  来源:igfitidea点击:

validation max_length, min_length in codeigniter

phpcodeignitervalidation

提问by cabita

I am getting an error when trying to set custom validation messages in CodeIgniterfor the min_lengthand max_lengthvalidation constraints.

尝试CodeIgnitermin_lengthmax_length验证约束设置自定义验证消息时出现错误。

These are my validation rules:

这些是我的验证规则:

$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]';

These are my custom validation message:

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

$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');

In my example we have two fields, but I have many fields with different minimum and maximum length values. They all have a specific constraint validations. This messages doesn't work. The message appears by default. Thanks for your help.

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

回答by mrsrinivas

Just add another %sto get the length value..

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

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

You will give the size only if you use %ssecond time. first time it will give field name you specified..

仅当您使用%s第二次时才给出尺寸。第一次它会给你指定的字段名称..

回答by stealthyninja

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

is missing a closing bracket -- ):

缺少右括号 -- )

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


Update
Perhaps using a callbackfunction would be better? E.g.:


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

$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;        
    }
}

回答by MY_Mark

When using set_message, you can't specify the lengths (both min and max) the way that you have done. You can either set a general message for min_lengthand max_lengthlike:

使用 时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');

Otherwise, you will have to utilise CodeIgniter's callback feature and set up a custom validation function which would look something like this (for the minimum length!!).

否则,您将不得不利用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;
    }
}

You would set your validation rule like this:

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

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

Notice the callback_prefix on the custom_min_lengthrule. This replaces, and is used instead of the usual min_lengthCI function.

注意规则的callback_前缀custom_min_length。这将取代并代替通常的min_lengthCI 功能。

Hope that helps...I'll leave you figure out how to do the custom_max_lengthfunction :)

希望有帮助......我会让你弄清楚如何做这个custom_max_length功能:)

回答by BitFix

If you want to show your own validation message, do something like that:

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

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

without the branches: [num]

没有分支:[num]

Greetings

你好