【问题标题】:CakePHP Validation Rule ErrorCakePHP 验证规则错误
【发布时间】:2011-12-03 17:51:12
【问题描述】:

我编写了一些验证函数来检查用户的电子邮件是否存在于系统中。

我收到以下错误

注意 (8):未定义的偏移量:0 [CORE/cake/libs/model/model.php,第 1122 行]

这是导致错误的代码

'email' => array(
            'emailRule-1' => array(
                'rule' => 'email',
                'message' => 'email format is incorrect',
                'last' => true
            ),
            'emailRule-2' => array(
                'rule' => 'checkEmailExist',
                'message' => 'email already exists in the system'
            )
        ),

而且 rule2 似乎是造成错误的原因,这里是 rule2:

function checkEmailExist($emailAddress, $user_id){
       $this->recursive = -1;
       if($user_id > 0){
           $user = $this->read(array('email'), $user_id);

           if($emailAddress == $user['User']['email'])
              return true;
       }


       $result = $this->find('count', array('conditions' => array('User.email' => $emailAddress)));
       return $result > 0 ? false : true;
    }

【问题讨论】:

    标签: php validation cakephp


    【解决方案1】:

    为什么不这样做呢?

    public $validate = array(
        'email' => array(
            'rule' => array('email', 'isUnique')
        )
    );
    

    您可能希望将其拆分为两个单独的规则以应用您自己的错误消息,但这应该可以正常工作。

    【讨论】:

      【解决方案2】:

      您是否尝试调试 $emailAddress 包含的内容? 我敢打赌这是一个数组^^

      function checkEmailExist($emailAddress, $user_id){
         $this->recursive = -1;
         $email = array_shift(emailAddress);
         ...
      

      你需要先获取子元素

      所以请记住:首先使用 debug() 或 pr() 调试变量总是一个好主意。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-01-08
        • 2012-07-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-02
        • 1970-01-01
        相关资源
        最近更新 更多