cakephp代码不适用于字母数字验证

时间:2013-12-13 07:08:46

标签: cakephp cakephp-2.4

我们已经尝试了几种验证密码的解决方案,但没有一种是有效的,但是用户登录后,除了密码中的字母数字验证外,所有验证都能正常工作。

这是代码:

'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

1 个答案:

答案 0 :(得分: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'
        )
    )
);
相关问题