2013-12-13 27 views
0

我們已經嘗試了幾種驗證密碼的解決方案,但都沒有工作,但用戶登錄後,除密碼中的字母數字驗證外,所有驗證都可以正常工作。cakephp代碼不能用於字母數字驗證

這裏是代碼:使用自定義功能,這是行不通的

'password' => array ('required' => array (

    'rule' => array ('notEmpty'), 
    'rule' => array ('between',1,15), 

    //'rule' => array('custom', '[a-zA-Z0-9, ]+'), 
    'message' => 'A password is required,must be between 8 to 15 characters') 
), 

因此我們嘗試在模型

'alphaNumeric' => array(
    'rule'  => array('alphaNumericDashUnderscore'), 
    'rule'  => 'alphaNumeric', 
    'required' => true, 
    'message' => 'password must contain Alphabets and numbers only' 
)), 

自定義函數

public function alphaNumericDashUnderscore($check) { 
    $value = array_values($check); 
    $value = $value[0]; 

    return preg_match('|^[0-9a-zA-Z_-]*$|', $value); 
} 

我們介紹CakePHP版本工作2.4.3

回答

4

這是因爲您在數組中定義了兩次相同的密鑰rule。第二個將總是覆蓋第一個。

按照documentation,你應該這樣做如下:

public $validate = array(
    'password' => array(
     'password-1' => array(
      'rule' => 'alphaNumeric', 
      'message' => 'password must contain Alphabets and numbers only', 
     ), 
     'password-2' => array(
      'rule' => 'alphaNumericDashUnderscore', 
      'message' => 'password must contain Alphabets and numbers only' 
     ) 
    ) 
);