これを実行して、テーブルでメールを検索できます。
// 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
)
)
);
上記のコードは正常に動作します。
データベースにクエリを実行する前に、電子メールが有効な電子メールであるか空であるかを検証することを考えていました。私は以下のものを思いつきました。私が抱えている問題は、 User を で設定すること$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'));
}
}
}
}
}
誰かが私が望むものと同様の解決策を実行しましたか?