2011-12-13 87 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]

問候