cab*_*ita 8 php validation codeigniter
我在尝试CodeIgniter为min_length和max_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]';
Run Code Online (Sandbox Code Playgroud)
这些是我的自定义验证消息:
$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');
Run Code Online (Sandbox Code Playgroud)
在我的例子中,我们有两个字段,但我有许多字段具有不同的最小和最大长度值.它们都有特定的约束验证.此消息不起作用.默认情况下会显示该消息.谢谢你的帮助.
mrs*_*vas 12
%s即可获得长度值..$this->form_validation->set_message('min_length', '%s: the minimum of characters is %s');
Run Code Online (Sandbox Code Playgroud)
只有在%s 第二次使用时才会给出尺寸.第一次它会给你指定的字段名称..
$this->form_validation->set_rules('password', 'Password', 'required|min_length[8]|max_length[20]';
Run Code Online (Sandbox Code Playgroud)
缺少一个结束括号 - ):
$this->form_validation->set_rules('password', 'Password', 'required|min_length[8]|max_length[20]');
Run Code Online (Sandbox Code Playgroud)
更新
也许使用回调函数会更好吗?例如:
$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;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
38709 次 |
| 最近记录: |