我可以这样做以在表格中搜索电子邮件。
// controller method
public function forgot_password() {
if ($this->Session->read('Auth.User')) {
$this->redirect(array('action' => 'add'));
} else {
if ($this->request->is('post') || $this->request->is('put')) {
$user = $this->User->findByEmail($this->request->data('User.email'));
if ($user) {
$this->request->data['User']['id'] = $user['User']['id'];
$this->request->data['User']['random_string'] = $this->String->random();
unset($this->request->data['User']['email']);
$this->User->save($this->request->data);
// $this->_sendEmail($user);
$this->Session->setFlash(__('Instructions has been sent to your email'), 'flash');
$this->redirect(array('action' => 'forgot_password'));
} else {
// passed! do stuff
}
}
}
}
// validate in the model
public $validate = array(
'email' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'An Email is required'
),
'email' => array(
'rule' => array('email'),
'message' => 'Email is invalid'
),
'isUnique' => array(
'rule' => array('isUnique'),
'message' => 'Email is already in use'
)
),
'password' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'A password is required'
)
),
'role' => array(
'valid' => array(
'rule' => array('inList', array('admin', 'author')),
'message' => 'Please enter a valid role',
'allowEmpty' => false
)
)
);
上面的代码工作正常。
我只是想在查询数据库之前验证电子邮件是有效电子邮件还是空电子邮件。我想出了下面的那个。我遇到的问题是将用户设置为$this->request->data
. 每当我验证时,它都会通过 isUnique 规则运行并失败。
public function forgot_password() {
if ($this->Session->read('Auth.User')) {
$this->redirect(array('action' => 'add'));
} else {
if ($this->request->is('post') || $this->request->is('put')) {
$this->User->set($this->request->data);
if ($this->User->validates()) {
$user = $this->User->findByEmail($this->request->data('User.email'));
if ($user) {
$this->request->data['User']['id'] = $user['User']['id'];
$this->request->data['User']['random_string'] = $this->String->random();
unset($this->request->data['User']['email']);
$this->User->save($this->request->data);
// $this->_sendEmail($user);
$this->Session->setFlash(__('Instructions has been sent to your email'), 'flash');
$this->redirect(array('action' => 'forgot_password'));
}
}
}
}
}
有人做过类似于我想要的解决方案吗?