yii2.0验证码第一次验证正确,切换了验证码之后再输入验证就不正确了 [ 2.0 版本 ]
控制器代码
<?php
namespace app\modules\pub\controllers;
use Yii;
use backend\base\BaseController;
use backend\modules\pub\models\LoginForm;
use backend\modules\pub\models\RegisterForm;
class DefaultController extends BaseController{
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'=>6, //最短为4位
'maxLength'=>6, //是长为4位
'transparent'=>true, //显示为透明
'testLimit'=>0,
'fixedVerifyCode' => YII_ENV_TEST ? 'test' : null,
],
];
}
//登录
public function actionIndex(){
if (!\Yii::$app->user->isGuest) {
return $this->goHome();
}
$model = new LoginForm();
if ($model->load(Yii::$app->request->post()) && $model->login()) {
return $this->goBack();
} else {
return $this->render('index', [
'model' => $model,
]);
}
}
//注册
public function actionRegister(){
$model = new RegisterForm();
if ($model->load(Yii::$app->request->post())) {
if ($user = $model->register()) {
if (Yii::$app->getUser()->login($user)) {
return $this->goHome();
}
}
}
return $this->render('register', [
'model' => $model,
]);
}
}
模型代码
<?php
namespace backend\modules\pub\models;
use app\modules\pub\models\YiiUser;
use backend\models\CommonModel;
use Yii;
class RegisterForm extends CommonModel{
public $username;
public $password;
public $password_rep;
public $email;
public $verifyCode;
//验证规则
public function rules(){
return [
//账号、密码、确认密码、邮箱、验证码必须
[['username','password','password_rep','email','verifyCode'],'required'],
//账号只能是汉字/数字/下划线,不能包含空格
['username','match','pattern'=>'/^[\x{4e00}-\x{9fa5}_a-zA-Z0-9]*$/'],
//用户名最大10位,最小3位
['username','string','max'=>16,'min'=>2],
//用户名/邮箱唯一
['username','unique','targetClass'=>'\backend\modules\pub\models\YiiUser','message'=>'账号已存在'],
['email','unique','targetClass'=>'\backend\modules\pub\models\YiiUser','message'=>'邮箱已被绑定'],
//去除空格
[['username','email'],'trim'],
//密码最大16位,最小6位
['password','string','max'=>16,'min'=>6],
//验证邮箱
['email','email','message'=>'邮箱不规范'],
//验证两次密码是否一致
['password_rep','compare','compareAttribute'=>'password','message'=>'两次密码不一致'],
//验证码
['verifyCode', 'captcha','captchaAction'=>'pub/default/captcha'],
];
}
public function attributeLabels(){
return [
'username'=>'用户名',
'password'=>'密码',
'password_rep'=>'确认密码',
'verifyCode'=>'验证码',
'email'=>'邮箱',
];
}
public function register(){
if ($this->validate()) {
$user = new YiiUser();
$user->username = $this->username;
$user->email = $this->email;
$user->setPassword($this->password);
$user->generateAuthKey();
if ($user->save()) {
return $user;
}
}
return null;
}
}
页面Form代码
<?php $form = ActiveForm::begin([
'id' => 'form-signin',
]); ?>
<!--<form id="form-signin" class="form-signin">-->
<section>
<?= $form->field($model,'username')->textInput(['placeholder'=>'只能由汉字/数字/下划线组成,不能包含空格'])->label('账号')?>
<!--<div class="input-group">
<?/*= $form->field($model,'username')->label('账号')*/?>
<input type="text" class="form-control" name="username" placeholder="用户名">
<div class="input-group-addon"><i class="fa fa-user"></i></div>
</div>-->
<?= $form->field($model,'password')->passwordInput(['placeholder'=>'密码'])->label('密码')?>
<?= $form->field($model,'password_rep')->passwordInput(['placeholder'=>'确认密码'])->label('确认密码')?>
<?= $form->field($model,'email')->textInput(['placeholder'=>'邮箱'])->label('邮箱')?>
<?= $form->field($model, 'verifyCode')->label('验证码')->widget(Captcha::className(), [
'options'=>['placeholder'=>'验证码'],
'captchaAction' => 'default/captcha',
'imageOptions'=>['style'=>'margin-top:-5px;',],
'template' => '<div class="row"><div class="col-lg-6">{input}</div><div class="col-lg-6">{image}</div></div>',
]) ?>
<!--<div class="input-group">
<input type="password" class="form-control" name="password" placeholder="密码">
<div class="input-group-addon"><i class="fa fa-key"></i></div>
</div>-->
</section>
<section class="controls">
<div class="checkbox check-transparent">
<!--<input type="checkbox" value="1" id="remember" checked>
<label for="remember">记住我</label>-->
</div>
<a href="#">忘记密码?</a>
</section>
<section class="log-in">
<?= Html::submitButton('注册', ['class' => 'btn btn-greensea']) ?>
<!--<button class="btn btn-greensea">登录</button>-->
<span>或</span>
<?= Html::a('登录',null,['href'=>Url::toRoute('default/index'),'class'=>'btn btn-slategray']) ?>
<!--<button class="btn btn-slategray">创建一个新账号</button>-->
</section>
<!--</form>-->
<?php ActiveForm::end(); ?>
还有验证用户名和邮箱唯一的规则是在提交表单的时候验证,还是表单失去焦点验证啊?
今天有空了,又拿出这个问题搞了一会,发现当刷新了验证码之后,yii.validation.js中captcha方法中$('body').data(options.hashKey)居然为空,打印出来$('body').data()一看,发现options.hashKey和data中的对应不上,一个有模块名pub,另一个没有,然后就翻验证码的源码,都看了个遍后,将页面中验证码和模型中都改成'captchaAction' => '/pub/default/captcha',这样,问题就解决了。至此那个所谓的hashkey的值就变成了yiiCaptcha//模块名/控制器名/控制器ID,注意,yiiCaptcha后面确实是两个斜杠!!!!!!!!
共 0 个回答
没有找到数据。
中国海军
注册时间:2015-05-06
最后登录:2017-04-17
在线时长:15小时44分
最后登录:2017-04-17
在线时长:15小时44分
- 粉丝7
- 金钱370
- 威望0
- 积分520