【发布时间】:2013-11-26 05:35:19
【问题描述】:
我有一个用 cakephp 开发的网站。 我有一个名为 User 的模型,如下所示:
class User extends AppModel {
public $name = 'User';
public $validate = array(
'username' => array(
'not_empty' => array(
'rule'=> 'notEmpty',
'message'=> 'Username not empty'
)
),
'email' => array(
'email_invalid' => array(
'rule' => 'email',
'message' => 'Invalid mail'
),
'email_unique' => array(
'rule' => 'isUnique',
'message' => 'Mail already exist inside database'
)
)
);
public function beforeSave(){
if (isset($this->data['User']['password'])){
$this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
}
}
}
在我的验证中,我有规则 email_unique 检查数据库内是否已经存在另一封电子邮件。
当我更新用户时,我会在我的控制器中进行此操作:
$this->User->id = $this->request->data['User']['id'];
if ($this->User->save($this->request->data)) {
$this->redirect (array ('action'=>'index'));
}
else{
$this->Session->write('flash_element','error');
$this->Session->setFlash ('Error');
}
它总是失败,因为电子邮件不是唯一的,而是相同的记录!
如果保存是更新而不是创建,我想知道逃避验证的最佳方法是什么? 或者类似:检查页面是否是编辑转义验证或者我不知道..也许有很多系统,我想知道什么对我的问题更正确。
谢谢
【问题讨论】: