验证码输入两次才能验证成功

刚学Yii,先试做一个注册系统,在输入验证码时,只有输入两次才能成功。看看我的代码吧:

UserController.php

public function actions()


{


   return array(


   	'captcha' => array(


		'class' => 'CCaptchaAction',


		'backColor' => 0xFFFFFF,


	),


	'page'=>array(


		'class'=>'CViewAction',


	),


   );


}


 */


public function accessRules()


{


	return array(


		array('allow',  // allow all users to perform 'index' and 'view' actions


			'actions'=>array('index','view','register', 'captcha'),


			'users'=>array('*'),


		),


		array('deny',  // deny all users


			'users'=>array('*'),


		),


	);


}





public function actionRegister()


{


      $model = new Users('register');


      if(isset($_POST['Users']))


      {


         $model->attributes = $_POST['Users'];


	 if($model->save())


	 {


	 }


      }


   $this->render('register', array('model'=>$model));


}

Users.php

public function rules()


{


   return array (


	array('username, password', 'required'),


	array('verifypassword, email', 'required', 'on' => 'register'),


	array('username', 'length', 'min'=>4, 'max' =>20),


	array('password', 'length', 'min'=>5, 'max' =>128),


	array('password', 'compare', 'compareAttribute'=>'verifypassword', 'on' => 'register'),


	array('email', 'email', 'on' => 'register'),


	array('verifycode', 'captcha', 'allowEmpty'=>!extension_loaded('gd')),


   );


}





protected function beforeSave()


{


   if(parent::beforeSave())


   {


      $this->createtime = $this->lastvisit = time();


      $this->status     = self::USER_STATUS_ACTIVE;


      $randomkey        = rand();


      $this->activationKey = md5($randomkey);


      $this->password      = md5($this->activationKey.$this->password);


      return true;


   }


   else


      return false;


}

请大家看看到底是哪里错了呢

知道了,是Ajax的问题。但要用Ajax的话,就不能使用验证码了吗?有别的解决方法吗?


public function actions()

{

		return array(

			// captcha action renders the CAPTCHA image displayed on the contact page

			'captcha'=>array(

				'class'=>'CCaptchaAction',

				'backColor'=>0xFFFFFF,

				'minLength'=>4,

                                'maxLength'=>4,

				'testLimit'=>999,//这里你改大一些

			),

		);

	}