正如標題所說,我需要驗證密碼字段,但它可以包含特定數量的大寫字母,特殊字符和數字。所以我創建了這樣的功能CakePHP自定義驗證 - 檢查特定數量的大寫字母,符號和數字
function password_verification($password){
$errors[];
$min_password_length = ConfigOptions::getValue('password_minimum_length');// minimum password length
$special_characters = ConfigOptions::getValue('password_minimum_symbols');// Minimum symbols
$numbers = ConfigOptions::getValue('password_minimum_numbers');// minimum numbers
$length = strlen($password);// length of inserted password
preg_match_all("/[A-Z]/", $password, $caps_match);
$caps_count = count($caps_match [0]);// number of uppercase letters in password
preg_match_all("/[a-z]/", $password, $small_match);
$small_count = count($small_match [0]);// number of lowercase letters in password
preg_match_all("/[0-9]/", $password, $num_match);
$num_count = count($num_match [0]);// number of digits in password
preg_match_all("/[^a-zA-Z0-9]/", $password, $symb_match);
$symb_count = count($symb_match [0]);
if($min_password_length > $length) {
$errors['password_length'] = __('Password cannot be shorter than '.$min_password_length.' characters');
}// if
if($uppercase > $caps_count) {
$errors['password_uppercase'] = __('Password cannot have less than '.$uppercase.' uppercase letters');
}// if
if($special_characters > $symb_count) {
$errors[] = __('Password cannot have less than '.$symb_count.' special characters');
}// if
if($numbers > $num_count) {
$errors[] = __('Paswword cannot have less than '.$num_count.' numbers');
}// if
return $errors[];
}
我把它放在控制器中。但是我想知道是否有任何方法可以將其用於模型驗證。
事實上,看一看:http://book.cakephp.org/2.0/en/models/data-validation.html#定製驗證規則 – iso27002
我真的無法回答我是否看過蛋糕簿...... – JohnDoe