登录尝试失败后,Yii Framework无法打印错误消息

登录尝试失败后,Yii Framework无法打印错误消息,yii,Yii,您好,登录尝试失败后,我无法在yii中打印错误消息,下面是登录时我的控制器、视图、loginform(模型)和UserIdentity类的代码。 首先是控制器的代码 public function actionLogin() { $model = new LoginForm(); // if it is ajax validation request if (isset($_POST['ajax']) && $_POST['ajax'] === 'logi

您好,登录尝试失败后,我无法在yii中打印错误消息,下面是登录时我的控制器、视图、loginform(模型)和UserIdentity类的代码。 首先是控制器的代码

public function actionLogin() {
    $model = new LoginForm();
    // if it is ajax validation request
    if (isset($_POST['ajax']) && $_POST['ajax'] === 'login-form') {
        echo CActiveForm::validate($model);
        Yii::app()->end();
    }
    // collect user input data
    if (isset($_POST['LoginForm'])) {
        $model->attributes = $_POST['LoginForm'];
        // validate user input and redirect to the previous page if valid
        if ($model->validate() && $model->login())
            $this->redirect(array('site/index'));
    }
    // display the login form
    $this->render('login', array('model' => $model));
}
在控制器之后,我的视图文件中有以下代码

<?php
            $form = $this->beginWidget('CActiveForm', array(
                'id' => 'login-form',
                'enableAjaxValidation' => true,
                'enableClientValidation' => true,
                'clientOptions' => array(
                    'validateOnSubmit' => true,
                ),
                'htmlOptions' => array(
                    'id' => 'form-contact'
                ),
            ));
            echo CHtml::errorSummary($model);
            ?>
            <div id="form-left">
                <label for="text-email">Email(<span style="color: red;">*</span>)</label><br />
                <?php echo $form->textField($model, 'email', array('class' => 'input')); ?><br />
                <label for="text-password">Password (<span style="color: red;">*</span>)</label><br />
                <?php echo $form->passwordField($model, 'password', array('class' => 'input')); ?><br />
                <br>
                <?php echo CHtml::submitButton('Login', array('class' => 'button', 'style' => 'margin-bottom:50px;')); ?>
            </div>
            <?php $this->endWidget(); ?>

电子邮件(*)

密码(*)


下面是登录表单模型的代码

<?php

/**
 * LoginForm.php
 *
 * @author: antonio ramirez <antonio@clevertech.biz>
 * Date: 7/22/12
 * Time: 8:37 PM
 */
class LoginForm extends CFormModel {

    public $email;
    public $password;
    private $_identity;
    private $_user = null;

    /**
     * Model rules
     * @return array
     */
    public function rules() {
        return array(
            array('password, email', 'required'),
            array('password', 'authenticate'),
        );
    }

    /**
     * Returns attribute labels
     * @return array
     */
    public function attributeLabels() {
        return array(
            'email' => Yii::t('labels', 'E-mail'),
            'password' => Yii::t('labels', 'Password'),
        );
    }

    /**
     * Authenticates user input against DB
     * @param $attribute
     * @param $params
     */
    public function authenticate($attribute, $params) {
        $this->_identity = new UserIdentity($this->email, $this->password);
        if (!$this->_identity->authenticate())
            $this->addError('password', 'Incorrect email or password.');
    }

    /**
     * Login
     * @return bool
     */
    public function login() {

        if ($this->_identity === null) {
            $this->_identity = new UserIdentity($this->email, $this->password);
            $this->_identity->authenticate();
        }
        if ($this->_identity->errorCode === UserIdentity::ERROR_NONE) {
            return true;
        }
    }

    /**
     * Returns
     * @return null
     */
    public function getUser() {
        if ($this->_user === null) {
            $attribute = strpos($this->email, '@') ? 'email' : 'email';
            $this->_user = User::model()->find(array('condition' => $attribute . '=:loginname', 'params' => array(':loginname' => $this->email)));
        }
        return $this->_user;
    }

}

我找到了解决办法。您需要更改
LoginForm
-模型。我在更改内容的地方插入了注释

首先将方法
验证
更改为:

public function authenticate($attribute, $params) {
    $this->_identity = new UserIdentity($this->email, $this->password);

    // Change from "if (!$this->_identity->authenticate())" to:
    if ($this->_identity->authenticate() !== UserIdentity::ERROR_NONE)
        $this->addError('password', 'Incorrect email or password.');
}
然后是方法
login
,因为调用login()时没有结果,并且存在身份验证问题:

public function login() {

        if ($this->_identity === null) {
            $this->_identity = new UserIdentity($this->email, $this->password);
            $this->_identity->authenticate();
        }
        if ($this->_identity->errorCode === UserIdentity::ERROR_NONE) {
            return true;
        }
        return false; // add this line for the case when authentication failed
}

登录失败时现在会发生什么?重定向到登录页面,不显示错误消息
public function login() {

        if ($this->_identity === null) {
            $this->_identity = new UserIdentity($this->email, $this->password);
            $this->_identity->authenticate();
        }
        if ($this->_identity->errorCode === UserIdentity::ERROR_NONE) {
            return true;
        }
        return false; // add this line for the case when authentication failed
}