yii之验证码
给登录表单添加验证码,登录表单使用的是LoginForm模型,首先:
这样就ok了!
总共会修改以下3个文件:model中文件/controller中/view中
1.在model层的文件中插入
public $verifyCode;
在rules()中插入
array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()),
在attributeLabels()中插入
'verifyCode' =>'验证码';
在rules()还要添加
1. array('allow',
2. 'actions'=>array('captcha'),
3. 'users'=>array('*'),
4. ),
2.public function actions()
{
return array(
// captcha action renders the CAPTCHA p_w_picpath displayed on the contact page
'captcha'=>array(
'class'=>'CCaptchaAction',
'backColor'=>0xFFFFFF, //背景颜色
'minLength'=>4, //最短为4位
'maxLength'=>4, //是长为4位
'transparent'=>true, //显示为透明
),
);
}
3. 最后在View层的文件中插入
<?php if(CCaptcha::checkRequirements()): ?>
<div>
<?php $this->widget('CCaptcha'); ?>
<?php echo $form->textField($model,'verifyCode');?>
</div>
<?php endif;?>
转载于:https://blog.51cto.com/dreamingo/1322139