首页 > 其他 > 详细

Yii验证码简单使用及

时间:2017-01-11 13:11:22      阅读:214      评论:0      收藏:0      [点我收藏+]

控制器:(写了貌似也没用,未解决验证码位数;位数可改核心代码)

public $layout = false;//隐藏导航
    public function actions(){
        return [
            // captcha action renders the CAPTCHA image displayed on the contact page
            ‘captcha‘=>[
                ‘class‘ => ‘yii\captcha\CaptchaAction‘,
                ‘backColor‘=>0xFFFFFF,  //背景颜色
                ‘minLength‘=>4,  //最短为4位
                ‘maxLength‘=>4,   //是长为4位
                ‘transparent‘=>true,  //显示为透明
                ‘testLimit‘=>0,
                ‘fixedVerifyCode‘ => YII_ENV_TEST ? ‘test‘ : null,
            ],
        ];
    }

模型层:

//需要定义verifyCode

Public $verifyCode

public function rules()
    {
        return [//验证码
            [‘verifyCode‘, ‘required‘, ‘message‘ => ‘验证码不可以为空‘],
            [‘verifyCode‘, ‘captcha‘],
        ];
    }

视图层:

use yii\captcha\Captcha;(需要应用这个小组件)

//使用表单组件和验证组件
use yii\widgets\ActiveForm;
use yii\captcha\Captcha;

//生成表单
$form = ActiveForm::begin([
    ‘id‘ => ‘contact-form‘,
    "action"=>["vip/az"]
    ]);
//显示验证码
echo $form->field($model, ‘verifyCode‘)->widget(Captcha::className(), [
    ‘template‘ => ‘<div class="row"><div class="col-lg-3">{image}</div><div class="col-lg-6">{input}</div></div>‘,
]) ;

echo "<input type=‘submit‘ value=‘提交‘/>";
ActiveForm::end();

Yii验证码简单使用及

原文:http://www.cnblogs.com/myon/p/6273072.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!