dmodai 2016-05-09 18:26:19 10848次浏览 4条评论 14 4 0

在UserForm中加入验证码rules

[['user', 'pwd', 'verifyCode'], 'required','message'=>'{attribute}不能为空!'],
 ['verifyCode', 'captcha','captchaAction'=>'/admin/site/captcha','message'=>'{attribute}有误'],  //验证验证码是否正确

这里的'captchaAction'=>'/admin/site/captcha'指定验证码验证方法,你可以使用默认site/controller下的

siteController下注册captcha方法

 public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                //'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
                //'backColor'=>0x000000,//背景颜色
                'maxLength' => 4, //最大显示个数
                'minLength' => 4,//最少显示个数
                //'padding' => 5,//间距
                'height'=>50,//高度
                'width' => 80,  //宽度
                // 'foreColor'=>0xffffff,     //字体颜色
                //'offset'=>4,        //设置字符偏移量 有效果
                //'controller'=>'login',        //拥有这个动作的controller
            ],
        ];
    }

参数请自行选择选择

重要的view层:点击更换验证码;

 <div class="form-group form-group-lg captcha field-userform-verifycode required has-error" >
                <label class="control-label" for="userform-verifycode">验证</label>
                <div class="row">
                    <div class="col-md-5 col-xs-5">
                        <input type="text"  class="form-control" name="UserForm[verifyCode]" >
                    </div>
                    <div class="col-md-3 col-xs-4 mr20">
                        <img id="imgVerifyCode" onclick="changeVerifyCode()"  alt="验证码">
                    </div>
                    <div class="col-md-3 col-xs-3">
                        <a style="cursor:pointer;vertical-align: middle;line-height: 40px;" onclick="changeVerifyCode()">换一张</a>
                    </div>
                </div>

                <p class="help-block help-block-error"><?php echo ($_POST && isset($model->errors['verifyCode'][0])) ? $model->errors['verifyCode'][0] : '';?></p>
            </div>

对应js点击更换图片,对于验证码得到则采用 /admin/site/captcha?refresh 这个方法

$(function(){                   //当页面加载的时候
    changeVerifyCode();         //刷新或者重新加载一个验证码

    //清除输入框里面的数据
    $("#clear_form").click(function(){
        $("input").val('');
    });
});

//更改或者重新加载验证码
function changeVerifyCode() {
    $.ajax({
        url: "/admin/site/captcha?refresh",
        dataType: 'json',
        cache: false,
        success: function(data) {
            $("#imgVerifyCode").attr('src', data['url']);
        }
    });
}

在视图中我没采用yii2的form,样式是仿写过来的,也可以显示错误;

如果觉得代码不够,可以参看

https://github.com/dmodaii/yiicms2

这个master是fork的,dev分支上才有我改的

觉得很赞
  • 评论于 2016-05-25 17:30 举报

    图片的src不能直接用那个src吗?为啥还要用一个ajax请求一次?

    3 条回复
    评论于 2016-05-25 20:33 回复

    当然你也可以一开始用的是src写死,然后点击再用ajax; 我这样一开始和以后的都是同样的一份代码(ajax)更方便不是

    评论于 2016-05-26 09:08 回复

    有点疑惑,ajax请求返回一个url,然后赋给img src,为啥不直接用这个url,给img src,ajax这一步是多余的

    评论于 2016-05-26 11:09 回复

    yii2的验证码你确实可以直接img src然后在验证码源码里面更改刷新页面可以更换验证码; 但是点击更换验证码实时更换你怎么做呢,你要显示验证码的url又从哪里来

    觉得很赞
  • 评论于 2016-07-11 09:23 举报

    默认点击验证码图片就能刷新验证码 你干嘛自己写?

    使用验证码的正确姿势-登录添加验证码

  • 评论于 2016-09-05 10:45 举报

    你好,我想用您的这个框架做app,就是给app写接口以及写app后台,我不太了解你写的这个框架,想问下您做app这个框架合不合适?

  • 评论于 2017-04-02 17:08 举报

    $('#VerifyCode').on('click', function() {

        this.src=this.src+"?n="+Math.random();
    });
    
您需要登录后才可以评论。登录 | 立即注册