我是yii的初学者。 我想在视图中使用Chtml并在模型中使用Cactiverecord进行身份验证但是不起作用并显示错误以获取正确的电子邮件并传递
我的观看代码是:login_form.php
<div class="form">
<?php echo Yii::app()->user->getFlash('notification');?>
<?php echo CHtml::beginForm(); ?>
<?php echo CHtml::errorSummary($model); ?>
<div class="row">
<?php echo CHtml::activeLabel($model,'user_email'); ?>
<?php echo CHtml::activeTextField($model,'user_email') ?>
</div>
<div class="row">
<?php echo CHtml::activeLabel($model,'user_password'); ?>
<?php echo CHtml::activePasswordField($model,'user_password') ?>
</div>
<div class="row">
<?php echo CHtml::activeLabel($model,'rememberMe'); ?>
<?php echo CHtml::activeCheckBox($model,'rememberMe') ?>
</div>
<div class="row submit">
<?php echo CHtml::submitButton('Send'); ?>
</div>
<?php echo CHtml::endForm(); ?>
</div><!-- form -->
我的控制器是:
public function actionLogin_form()
{
$model=new Client;
// 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['Client']))
{
$model->attributes=$_POST['Client'];
// validate user input and redirect to the previous page if valid
if($model->validate() && $model->login()) //&& $model->client() && $model->login()
{
print_r($_REQUEST);
//echo Yii::app()->user->isGuest;
//return;
//$this->redirect('Yii::app()->createAbsoluteUrl'); //Yii::app()->user->returnUrl
}else echo "NO_";
}
// display the login form
$this->render('login_form',array('model'=>$model));
}
模型中的:Client.php
<?php
class Client extends CActiveRecord
{
public $user_email;
public $user_password;
public $rememberMe;
private $_identity;
public function tableName()
{
return 'user';
}
public function rules()
{
return array(
array('user_email','authenticate'),
//array('username','password', 'length', 'max'=>30), ,'user_password'
//array('user_email','unique'),
array('rememberMe', 'boolean'),
// password needs to be authenticated
array('user_password', 'authenticate'),
);
}
public function attributeLabels()
{
return array(
'user_id' => 'id',
'user_email' => 'email',
'user_password' => 'password',
'rememberMe'=>'Remember me next time',
);
}
public function authenticate($attribute,$params)
{
if(!$this->hasErrors())
{
$this->_identity=new UserIdentity($this->user_email,$this->user_password);
if(!$this->_identity->authenticate())
$this->addError('user_password','Incorrect email or password.');
$this->addError('user_email','Incorrect email or password.___');
}
}
public function login()
{
if($this->_identity===null)
{
$this->_identity=new UserIdentity($this->user_email,$this->user_password);
$this->_identity->authenticate();
}
if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
{
$duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
Yii::app()->user->login($this->_identity,$duration);
return true;
}
else
return false;
}
public static function model($className=__CLASS__)
{
return parent::model($className);
}
}#end of class
?>