CakePHP 验证规则错误
我编写了一些验证函数来检查系统中是否存在用户的电子邮件。
我收到以下错误
通知(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不这样做呢?
您可能希望将其分成两个单独的规则来应用您自己的错误消息,但这应该可以正常工作。
Why not do it like this?
You might want to split it up into two separate rules to apply your own error messages, but this should work just fine.
您是否尝试调试 $emailAddress 包含的内容?
我敢打赌这是一个数组^^,
您需要首先获取子元素,
因此请记住:首先使用 debug() 或 pr() 来调试变量总是一个好主意。
Did you try to debug what $emailAddress contains?
I bet this is an array^^
you need to get the child element first
so remember: always a good idea to use debug() or pr() to debug your variables first.