CakePHP自定义验证-检查大写字母、符号和数字的具体数量

CakePHP自定义验证-检查大写字母、符号和数字的具体数量,php,cakephp,cakephp-2.0,Php,Cakephp,Cakephp 2.0,正如标题所说,我需要验证密码字段,但它可以包含特定数量的大写字母、特殊字符和数字。所以我创建了这样的函数 function password_verification($password){ $errors[]; $min_password_length = ConfigOptions::getValue('password_minimum_length');// minimum password length $special_characters = ConfigOptions::getVa

正如标题所说,我需要验证密码字段,但它可以包含特定数量的大写字母、特殊字符和数字。所以我创建了这样的函数

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[];
}

我把这个放在控制器里。但我想知道是否有任何方法可以将此应用到模型验证中。

为每个条件创建一个验证规则。以下是您需要在模型中添加的内容的一般概念:

public $validate = array(
    'password' => array(
            'id_rule_1' => array(
                'rule' => 'isPasswordGreaterThanMinLenght'
            ),
            'id_rule_2' => array(
                'rule' => 'isCapsCountLimitReached'
            ),
        ));


public function isPasswordGreaterThanMinLenght($check){
    $min_password_length = ConfigOptions::getValue('password_minimum_length');// minimum password length
    $length = strlen($this->data['password']);// length of inserted password
    $this->validator()->getField('password')->getRule('id_rule_1')->message = 'Password cannot be shorter than '.$min_password_length.' characters';
    return ($min_password_length > $length);
}

public function isCapsCountLimitReached($check){
    $uppercase = '';//define uppercase here
    $password = $this->data['password'];
    preg_match_all("/[A-Z]/", $password, $caps_match);
    $caps_count = count($caps_match [0]);// number of uppercase letters in password
    $this->validator()->getField('password')->getRule('id_rule_2')->message = 'Password cannot have less than '.$uppercase.' uppercase letters';
    return ($uppercase > $caps_count);
}

这并不能回答这个问题。若要评论或要求作者澄清,请在他们的帖子下方留下评论。我知道如何做,但我还需要检查是否有最低数量的数字和符号。事实上,请看一看:我真的无法回答,我看过cakebook了吗。。。