0

ログインフォームを検証するために、cakephpの検証を長い間チェックしてきました。私の問題は、ユーザー名とパスワードを空白として入力すると、検証が表示されないことです。

UserController.phpのログイン関数には

 if ($this->request->is('post')) {
            $this->user->set($this->request->data);
            $errors = $this->user->invalidFields(); 

            if ($this->Auth->login()) {
                return $this->redirect($this->Auth->redirect());
            } else {
                $this->Session->setFlash($this->Auth->authError, 'default', array(), 'auth');
                $this->redirect($this->Auth->loginAction);
            }
        } else {

            if ($this->Auth->login()) {
                return $this->redirect($this->Auth->redirect());
            }
        }

私のUser.phpモデルにはバリデーターが含まれています

 public $validate = array(
        'username' => array(
            'isUnique' => array(
                'rule' => 'isUnique',
                'message' => 'The username has already been taken.',
            ),

            'notEmpty' => array(
                'rule' => 'notEmpty',
                'message' => 'This field cannot be left blank.',
            ),
        ),
        'email' => array(
            'email' => array(
                'rule' => 'email',
                'message' => 'Please provide a valid email address.',
            ),
            'isUnique' => array(
                'rule' => 'isUnique',
                'message' => 'Email address already in use.',
            ),
        ),
        'password' => array(
            'rule' => array('minLength', 6),
            'message' => 'Passwords must be at least 6 characters long.',
        ),
        'current_password' => array(
            'rule' => '_identical',
            ),
        'name' => array(
            'rule' => 'notEmpty',
            'message' => 'This field cannot be left blank.',
        ),
    );

実際、私のログインフォームにはusgernameとパスワードしか含まれていません。しかし、私はユーザー登録フォームにこの検証を設定しました。検証は登録フォームで正しく機能しましたが、ログインの場合、検証は機能しませんでした。はい、同じ問題に関してこのWebサイト自体に多くの質問が投稿されていることを知っていますが、私の問題を解決するものは何もありません。私はすべてのstackoverflowの質問を試しました。助けてください

4

1 に答える 1

0

検証は、保存するとき、または以下を使用して検証メソッドを直接呼び出すときにのみ発生します。

$this->Model->validates();

実際にデータを検証していないため、検証エラーは発生しません。検証エラーを表示するには、次の手順を実行する必要があります。

if ($this->request->is('post')) {
        $this->User->set($this->request->data);
        if ($this->User->validates()) {
             echo "This is valid!";
        } else {
             echo "This is invalid";
             $errors = $this->User->validationErrors;
        }
} 
于 2012-12-10T20:46:11.447 に答える