CCaptcha能图片出来,但是不能验证

我是在[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的时候,它不会提示是错误的呢?是还有哪里不对吗?

还没有搞掂呢~~~can somebody help me?

你的验证规则是在 ‘on’=>‘login’ 条件下才执行的,明白?

$user->attributes = $_POST[‘User’];

$user->setScenario(‘login’);

$user->validate();

首先谢谢你的回答。

按照你这样说的话,如果我把’on’=>'login’去掉,或者是使用你所说的$user->setScenario(‘login’);就可以使用了吧,

但是我进行上面两种方法,无论是不输入,还是输入错误的验证码,效果还是没有出来~

现在测试的时候问题更加严重了,

/modules/admin/controllers/UserController.php里面




$user = new User;

if(!empty($_POST['User']))

{

    $user->attributes = $_POST['User'];

    var_dump($user->save());

}



$user是可以实例化的,还有就是,

$user->attributes = $_POST[‘User’];

这句之后,

var_dump($user);

是各个属性都有值的。

注意,最后一行输出是bool(false)的,晕死了~~~~到底是哪里出现问题了呢?难道是放到modules的问题?

[color="#FF0000"]

ok,不好意思,这个问题解决了,原来是我数据表里面有些字段是不能为空的,如果直接使用save()的话,由于没有把主键传过去,认为是一条新记录,加上不是所有的属性都已经赋值,所以才出现了false。

[/color]

[color="#0000FF"]

不过验证码的问题还没有搞掂啊,哪位可以帮帮我啊~

[/color]

我用的是1.1.x,对1.0.x,不知道从哪下手了,呵呵

我查了一下,估计是你

‘captchaAction’=>‘site/captcha’, 的问题

captchaAction的值应为当前controller 中某一个action的名称,你命名为site/captcha显然不对

先谢谢你的回答。

你说的情况是没有影响的,因为那个意思是加载另外一个controller里面的验证码而已,我把它去掉之后是一样的。

怎么办啊怎么办啊~~~

你的rules里舍得是’on’=>‘login’,但是你却没有这样设置model的scenario。

另外,1.1里是没有safeAttributes的,你必须在rules里声明。具体参见guide。

不是这样的啦,强哥,

我把’on’=>'login’去掉了,还是不行的,

我用的是1.0.11,要设置safeAttributes才可以对verifyCode传值的

我刚才试了一下,如果在/modules/admin/user/login里面直接使用models/ContactForm.php的话,那个验证程序又可以正常验证的,意思是说,可能是我的/models/User.php写错了,但是我看来看去都觉得没有错啊!




<?php


class User extends CActiveRecord

{

	/**

	 * The followings are the available columns in table 'User':

	 * @var integer $id

	 * @var string $username

	 * @var string $password

	 * @var integer $role_id

	 * @var integer $status

	 * @var integer $points

	 */

	 public $verifyCode;


	/**

	 * Returns the static model of the specified AR class.

	 * @return CActiveRecord the static model class

	 */

	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}


	/**

	 * @return string the associated database table name

	 */

	public function tableName()

	{

		return 'User';

	}


	/**

	 * @return array validation rules for model attributes.

	 */

	public function rules()

	{

		return array(

			array('username','length','max'=>63),

			array('password','length','max'=>127),

			array('username, password, role_id, status, points', 'required'),

			array('role_id, status, points', 'numerical', 'integerOnly'=>true),

			array('verifyCode', 'captcha', 

	//			'captchaAction'=>'site/captcha', 

	//			'on'=>'login', 

                'allowEmpty'=>!Yii::app()->user->isGuest || !extension_loaded('gd')

			),

		);

	}


	/**

	 * @return array relational rules.

	 */

	public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

			"userdetail"=>array(

				self::HAS_ONE,

				"Userdetail",

				"user_id",

			),

			"items"=>array(

				self::HAS_MANY,

				"Item",

				"UserItem(user_id, item_id)",

			),

			"invoices"=>array(

				self::HAS_MANY,

				"Invoice",

				"UserInvoice(user_id, invoice_id)"

			),

		);

	}


	/**

	 * @return array customized attribute labels (name=>label)

	 */

	public function attributeLabels()

	{

		return array(

			'id' => 'Id',

			'username' => 'Username',

			'password' => 'Password',

			'role_id' => 'Role',

			'status' => 'Status',

			'points' => 'Points',

			'verifyCode'=>'verify code',

		);

	}


	/**

	 * @return array attributes that can be massively assigned

	 */

	public function safeAttributes()

	{

		return array(

			'username',

			'password',

			'verifyCode',

		);

	}

}



你的captchaAction没设对?从你的描述上看应该是:‘captchaAction’=>’/admin/user/captcha’

capthchaAction是没有问题的吧,因为图片能显示出来了,但是我输入错误的或者是空的验证吗的时候,这个验证都不起作用的,我试过在/modules/admin/user/views/login.php里面,

<?php $this->widget(‘CCaptcha’, array(‘captchaAction’=>"/admin/user/captcha"));?>

结果还是一样,

郁闷~不知道哪里错了

我说的是rules里的。

已经设了,还是不行~~~囧




<?php


class User extends CActiveRecord

{

	/**

	 * The followings are the available columns in table 'User':

	 * @var integer $id

	 * @var string $username

	 * @var string $password

	 * @var integer $role_id

	 * @var integer $status

	 * @var integer $points

	 */

	 public $verifyCode;


	/**

	 * Returns the static model of the specified AR class.

	 * @return CActiveRecord the static model class

	 */

	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}


	/**

	 * @return string the associated database table name

	 */

	public function tableName()

	{

		return 'User';

	}


	/**

	 * @return array validation rules for model attributes.

	 */

	public function rules()

	{

		return array(

			array('username','length','max'=>63),

			array('password','length','max'=>127),

			array('username, password, role_id, status, points', 'required'),

			array('role_id, status, points', 'numerical', 'integerOnly'=>true),

			array('verifyCode', 'captcha', 

                'captchaAction'=>'/admin/user/captcha',

	//			'captchaAction'=>'site/captcha', 

	//			'on'=>'login', 

                'allowEmpty'=>!Yii::app()->user->isGuest || !extension_loaded('gd')

			),

		);

	}


	/**

	 * @return array relational rules.

	 */

	public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

			"userdetail"=>array(

				self::HAS_ONE,

				"Userdetail",

				"user_id",

			),

			"items"=>array(

				self::HAS_MANY,

				"Item",

				"UserItem(user_id, item_id)",

			),

			"invoices"=>array(

				self::HAS_MANY,

				"Invoice",

				"UserInvoice(user_id, invoice_id)"

			),

		);

	}


	/**

	 * @return array customized attribute labels (name=>label)

	 */

	public function attributeLabels()

	{

		return array(

			'id' => 'Id',

			'username' => 'Username',

			'password' => 'Password',

			'role_id' => 'Role',

			'status' => 'Status',

			'points' => 'Points',

			'verifyCode'=>'verify code',

		);

	}


	/**

	 * @return array attributes that can be massively assigned

	 */

	public function safeAttributes()

	{

		return array(

			'username',

			'password',

			'verifyCode',

		);

	}

}



你的view里没有对应verifyCode的error()? 另外,把errorSummary也显示出来看看?

在/modules/admin/UserController.php中使用了CHtml::errorSummary($user);

啊!我吐血啦! :blink:

居然是已经验证的了的!

不过好奇怪啊,我这样




if($user->validate())

            {

                echo "ok";

            }



怎么没有输出的?

很感谢qiang跟 qq71151461的耐心回答,谢谢~

再次感谢qiang跟 qq71151461的耐心回答

问题已经找出来了,原来我在models/User.php的rules()里面设置的问题




public function rules()

{

    return array(

        //其它

        array('username, password, role_id, status, points', 'required'),

        //其它

    );

}



注意的是,在登录的时候, role_id, status, points这三个属于是为空的!所以导致了一直出来上面的问题,

这个的大部分代码是由yiic.php生成的,所以没有想到是这个原因~~