如何修复未定义的偏移量和无法找到名称的验证处理程序


How to fix Undefined offset and Could not find validation handler for name

我的验证代码是这样的:

public $validate = array(
    'name' => array(
        'rule' =>array(
            'rule'=>'notEmpty',
            'required' => true ,
            'allowEmpty' => false ,
            'on'=>'create',
            'message' => 'This field cannot be left empty'
        )
    ),
    'email' => array(
        'rule1' => array(
            'rule' => 'isUnique',
            'required' => true,
            'allowEmpty' => false,
            'on'=>'create',
            'message' => 'This email has already been taken.'
         ),
        'rule2' => array(
            'rule' => array('email'),
            'required' => true,
            'on'=>'create',
            'message' => 'Please supply a valid email address.'
        ),
    ),
    'password' => array(
        'Not Empty' => array(
            'rule' => 'notEmpty',
            'required' => true,
            'message' => 'Password must not be empty.'
        ),
        'password_contains' => array(
            'rule'=>'alphaNumeric',
            'message' => 'Password must contain letters and numbers'
        ),
        'password_length' => array(
            'rule' => array('lengthBetween', 5, 15),
            'required' => true,
            'message' => 'Passwords must be between 5 and 15 characters long.'
        ),
        'passwords match' => array(
            'rule' => 'matchPasswords',
            'message' => 'Your passwords do not match'
        )
    ),
    'password_again' => array(
        'Not Empty' => array(
            'rule' => 'notEmpty',
            'required' => true,
            'message' => 'Password must not be empty.'
        ),
    )
);
public function matchPasswords($data)
{
    if ($data['password'] == $this->data['User']['password_again']) {
        return true;
    } else {
        $this->invalidate('password_again', 'Your passwords do not match');
        return false;
    }
}

但是每次我运行代码,我得到这些错误:

通知(8):未定义偏移量:0 [CORE'Cake'Model'Validator'CakeValidationRule.php,行342]警告(512):无法找到名称的验证处理程序[CORE'Cake'Model'Validator'CakeValidationRule.php, line 281]

我不明白是什么问题。任何帮助都将不胜感激。谢谢! !

问题在这里

'name' => array(
    'rule' => array(
        'rule'=> 'notEmpty',
        //...
    )
)

把第一个rule键改成别的

'name' => array(
    'validName' => array(
        'rule' => array('notEmpty'),
        //...
    )
)

这些错误的原因如下。当找到rule键时,Cake尝试从其值解析验证处理程序的名称。在第二个例子中,它试图从array('notEmpty')中获取索引0处的值,该值被解析为notEmpty方法,一切正常。回到原来的name规则,Cake试图从array('rule'=>'notEmpty',...)获得索引0的值,没有这样的索引(偏移量),无法找到验证处理程序并触发错误。