【问题标题】:How to fix Undefined offset and Could not find validation handler for name如何修复未定义的偏移量和找不到名称的验证处理程序
【发布时间】:2015-05-28 17:28:43
【问题描述】:

我的验证码是这样的:

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,第 281 行] 的验证处理程序

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

【问题讨论】:

    标签: php validation cakephp


    【解决方案1】:

    问题来了

    '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 处的值,没有这样的索引(偏移量),无法找到验证处理程序并触发错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 2017-08-30
      • 1970-01-01
      • 2011-03-13
      • 1970-01-01
      • 2019-01-14
      相关资源
      最近更新 更多