-2

これを実行して、テーブルでメールを検索できます。

// 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'));
                }
            }
        }
    }
}

誰かが私が望むものと同様の解決策を実行しましたか?

4

2 に答える 2

1

コントローラーからこれを行うことができます。

App::uses('Validation', 'Utility');
if (Validation::email($this->request->data('User.email'))) {
  ...
}
于 2012-11-01T01:50:58.587 に答える
0

私は@Daveに同意します。あなたは、自分が何をしようとしているのかはっきりしていないということです。

まず、リクエストデータへのアクセスはおそらく$ this-> request-> data ['User']['email'];である必要があります。標準フォームヘルパー形式でデータを転記する場合。

値が空の場合、または電子メールルールが失敗した場合、save呼び出しでデータベースにクエリを実行する前に、モデルの検証が失敗することは間違いありません。notEmptyは最初のルールであるため、データベースにクエリを実行する前に、検証ユーティリティによってチェックされます。失敗した場合、フィールドはスキップされ、db呼び出しは行われません(「last」がfalseに等しくない限り。cakeはそのルールに正規表現パターンを使用するため、同じことが電子メールルールに適用されます。一連のイベントを追跡できます。 Model.phpの保存機能を確認します。

空でなく、電子メールルールが合格した場合、最終的にはデータベースにクエリを実行して、アドレスがすでに存在するかどうかを確認する必要があります。

于 2012-11-01T02:13:41.623 に答える