我是在[color="#FF0000"]/modules/admin[/color]里面做这个的,
/models/User.php
<?
class User extends CActiveRecord
{
public $verifyCode;
public static function model($className=__CLASS__)
{
return parent::model($className);
}
public function tableName()
{
return 'User';
}
public function rules()
{
return array(
array('verifyCode', 'captcha',
'captchaAction'=>'site/captcha',
'on'=>'login', 'allowEmpty'=>!Yii::app()->user->isGuest || !extension_loaded('gd')
),
);
}
public function relations()
{
return array();
}
public function attributeLabels()
{
return array(
'id' => 'Id',
'username' => 'Username',
'password' => 'Password',
'role_id' => 'Role',
'verifyCode'=>'verify code',
);
}
public function safeAttributes()
{
return array(
'username',
'password',
'verifyCode',
);
}
}
/modules/admin/controllers/UserController.php
public function actions()
{
return array(
'captcha'=>array(
'class'=>'CCaptchaAction',
'backColor'=>0xF5F5F5,
),
);
}
public function accessRules()
{
return array(
array('allow',
'actions'=>array('login', 'captcha'),
'users'=>array('*'),
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}
public function actionLogin()
{
//$this->_checkLogin();
$user = new User;
if(!empty($_POST))
{
$user->attributes = $_POST['User'];
$user->validate();
$identity = new UserIdentity($user->username, $user->password);
$identity->authenticate();
switch($identity->errorCode)
{
case UserIdentity::ERROR_NONE:
Yii::app()->user->login($identity);
//$this->_checkLogin();
break;
case UserIdentity::ERROR_USERNAME_INVALID:
case UserIdentity::ERROR_PASSWORD_INVALID:
$user->addError("password", "Invalid username or password!");
break;
default : break;
}
}
/modules/admin/views/login.php
<h1>Administrator Login</h1>
<?php echo CHtml::beginForm();?>
<p>
<label>Username </label><br/>
<?php echo CHtml::activeTextField($user, 'username', array('class'=>'title'));?>
</p>
<p>
<label>Password </label><br/>
<?php echo CHtml::activePasswordField($user, 'password', array('class'=>'title'));?>
<?php echo CHtml::error($user, 'password');?>
</p>
<p>
<label>verifyCode</label>
<?php $this->widget('CCaptcha');?>
<?php echo CHtml::activeTextField($user, 'verifyCode');?>
</p>
<br />
<?php echo CHtml::submitButton('Submit')?>
<?php echo CHtml::endForm();?>
<hr />
图片能出来了,但是为什么我输入错误或者是空的verify code的时候,它不会提示是错误的呢?是还有哪里不对吗?