0

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

这是代码:

'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

1 回答 1

4

rule那是因为您在数组中定义了两次相同的键。第二个总是会覆盖第一个。

根据文档,您应该执行以下操作:

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'
        )
    )
);
于 2013-12-13T07:39:41.783 回答