2011-12-13 91 views
7

尝试在CodeIgniter中为min_lengthmax_length验证约束设置自定义验证消息时出现错误。验证codeigniter中的max_length,min_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'); 

在我的例子中,我们有两个领域,但我有不同的最小和最大长度诸多领域值。他们都有一个特定的约束验证。此消息不起作用。该消息默认显示。谢谢你的帮助。

回答

8
$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;   
    } 
} 
+0

在我的源代码中没关系。当我做我的职位时,我忘记了关闭沮丧。我的怀疑继续。感谢您的更正。 – cabita

+0

@cabita - 我已经更新了我的答案。 – stealthyninja

+0

谢谢,我会审查你的答案。谢谢。 – cabita

2

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

通知之callback_前缀的custom_min_length规则。这取代了通常使用的min_length CI功能。

希望帮助...我会离开你弄清楚如何做custom_max_length功能:)

+0

感谢您的帮助,我会审查您的答案。 – cabita

12

它是简单的一个添加另一%s得到长度值..

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

只有在第二次使用%s时,您才会给出尺寸。你第一次指定它会给字段名..

0

如果你想显示自己的验证信息,做这样的事情:

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

没有分支:[NUM]

问候