yii注册

注册会使用验证码,下面是验证码的使用方法:

Yii中如何使用验证码:

在Model层的文件中插入

  public $verifyCode;

  在rules()中插入

  array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()),

       在attributeLabels()中插入

  'verifyCode' =>'验证码';

在Controller层的文件中插入

      public function actions()
 {
  return array(
   // captcha action renders the CAPTCHA image displayed on the contact page
   'captcha'=>array(
    'class'=>'CCaptchaAction',
    'backColor'=>0xFFFFFF,

   ),

  );
 }

最后在View层的文件中插入

     <?php if(CCaptcha::checkRequirements()): ?>
        <div class="row">
                <?php echo $form->labelEx($model,'verifyCode'); ?>
            <div>
                <?php $this->widget('CCaptcha'); ?>
                <?php echo $form->textField($model,'verifyCode');?>
     </div>
  <div class="hint">请输入验证码中的图片
  <br/>不区分大小写
                </div>
  <?php echo $form->error($model,'verifyCode'); ?>
       </div>
       <?php endif;?>

 

C:TestController.php/actionReg方法

	public function actionReg()
	{
		$model=new TestReg('reg');
		if(isset($_POST['TestReg'])) {
		$aa=$model->attributes = $_POST['TestReg'];
		$model->validate();

		//$model->getErrors();
			if($model->save()) {
			$this->redirect(array('test/login'));
			}else{echo '333';}
		}

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

 

M:TestReg.php模型

<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of TestLogin
 *
 * @author Administrator
 */
class TestReg extends CActiveRecord{
public $username;
public $password;
public $password2;
public $email;
public $verifyCode;
	

	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return User 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 'tbl_user';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('username,email', 'unique'),
			array('username, password,password2, email,verifyCode', 'required'),
			array('username password', 'length', 'min'=>3, 'max'=>18),
			array('password2', 'compare', 'compareAttribute' => 'password','on'=>'reg'),
//这里的'on'=>'reg'与控制器中$model=new TestReg('reg');相关。 //array('password2', 'compare', 'compareAttribute' => 'password'), array('verifyCode', 'captcha'), array('email', 'length','max'=>128), // The following rule is used by search(). // Please remove those attributes that should not be searched. //array('id, username, password, email', 'safe', 'on'=>'search'), ); } /** * @return array customized attribute labels (name=>label) */ public function attributeLabels() { return array( 'username' => 'Username', 'password' => 'Password', 'password2' => 'Password Again', 'email' => 'Email', 'verifyCode' =>'VerifyCode', ); } protected function beforeSave() { if($this->isNewRecord) { $this->password = md5($this->password); } return parent::beforeSave(); } } ?>

 

V:reg.php视图

<div class="form">
    <div class="row">
        <?php echo $form->label($model,'username'); ?>
        <?php echo $form->textField($model,'username') ?>
		<?php echo $form->error($model,'username'); ?>
    </div>
 
    <div class="row">
        <?php echo $form->label($model,'password'); ?>
        <?php echo $form->passwordField($model,'password') ?>
		<?php echo $form->error($model,'password'); ?>
    </div>
	
    <div class="row">
        <?php echo $form->label($model,'password2'); ?>
        <?php echo $form->passwordField($model,'password2') ?>
		<?php echo $form->error($model,'password2'); ?>
    </div>
 
    <div class="row">
        <?php echo $form->label($model,'email'); ?>
        <?php echo $form->textField($model,'email'); ?>
		<?php echo $form->error($model,'email'); ?>
    </div>
	

    <div class="row">
		<?php echo $form->labelEx($model,'verifyCode'); ?>
		<?php echo $form->textField($model,'verifyCode');?>
        <div><?php $this->widget('CCaptcha'); ?></div>
  <?php echo $form->error($model,'verifyCode'); ?>
	</div>
 
 
    <div class="row submit">
        <?php echo CHtml::submitButton('注册',array('class'=>'btn btn-large')); ?>
    </div>
 
<?php $this->endWidget(); ?>
</div><!-- form -->

 

posted @ 2013-05-03 23:33  jami918  阅读(261)  评论(0编辑  收藏  举报