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

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

但每次我 运行 我收到这些错误的代码:

Notice (8): Undefined offset: 0 [CORE\Cake\Model\Validator\CakeValidationRule.php, line 342] Warning (512): Could not find validation handler for name [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') 获取 index 0 处的值,解析为 notEmpty 方法,一切正常。
回到原来的 name 规则,Cake 尝试从 array('rule'=>'notEmpty',...) 获取 index 0 处的值,没有这样的索引(偏移量),验证处理程序不能找到并触发错误。