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.
在我的示例中,我们有两个字段,但是我有许多字段具有不同的最小和最大长度值。它们都有特定的约束验证。此消息不起作用。默认情况下会显示该消息。谢谢你的帮助。
4 个解决方案
#1
12
It's simple one 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时才会给出大小。第一次它会给你指定的字段名称..
#2
9
$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 callback function 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;
}
}
#3
2
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.
注意custom_min_length规则上的callback_前缀。这取代了,而不是通常的min_length CI函数。
Hope that helps...I'll leave you figure out how to do the custom_max_length
function :)
希望有帮助...我会告诉你如何做custom_max_length函数:)
#4
0
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
问候
#1
12
It's simple one 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时才会给出大小。第一次它会给你指定的字段名称..
#2
9
$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 callback function 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;
}
}
#3
2
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.
注意custom_min_length规则上的callback_前缀。这取代了,而不是通常的min_length CI函数。
Hope that helps...I'll leave you figure out how to do the custom_max_length
function :)
希望有帮助...我会告诉你如何做custom_max_length函数:)
#4
0
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
问候