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
validation max_length, min_length in codeigniter
提问by cabita
I am getting an error when trying to set custom validation messages in CodeIgniter
for the min_length
and max_length
validation constraints.
尝试CodeIgniter
为min_length
和max_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 %s
to 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 %s
second 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_length
and max_length
like:
使用 时set_message
,您无法按照您的方式指定长度(最小值和最大值)。您可以设置一般的消息min_length
和max_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_length
rule. This replaces, and is used instead of the usual min_length
CI function.
注意规则的callback_
前缀custom_min_length
。这将取代并代替通常的min_length
CI 功能。
Hope that helps...I'll leave you figure out how to do the custom_max_length
function :)
希望有帮助......我会让你弄清楚如何做这个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
你好