CakePHP 验证规则错误

发布于 2024-12-19 04:00:31 字数 1039 浏览 0 评论 0原文

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

我收到以下错误

通知(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 似乎是造成该错误的原因,并且这是规则2:

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

I've go some validation functions written to check if the user's email exists in the system.

I am getting the following error

Notice (8): Undefined offset: 0 [CORE/cake/libs/model/model.php, line 1122]

This is the code which causes the error

'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'
            )
        ),

And rule2 seems to be responsible for the error, and here is the 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;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薄荷梦 2024-12-26 04:00:31

为什么不这样做呢?

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

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

Why not do it like this?

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

You might want to split it up into two separate rules to apply your own error messages, but this should work just fine.

时常饿 2024-12-26 04:00:31

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

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

您需要首先获取子元素,

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

Did you try to debug what $emailAddress contains?
I bet this is an array^^

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

you need to get the child element first

so remember: always a good idea to use debug() or pr() to debug your variables first.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文