表单启用ajax验证后,提交显示验证码错误 [ 2.0 版本 ]
验证码错误
- 1.本来是正确的,但是我开启了'enableAjaxValidation' => true,'enableClientValidation' => true,这两个之后,填写完验证码是没有错误提示的,但是当我提交的时候显示验证码错误
- 1.view部分
<?php $form = ActiveForm::begin([
'id' => 'form-register',
'options' => ['class'=>'form-inline'],
'enableAjaxValidation' => true,
'enableClientValidation' => true,
]);?>
<?= $form->field($model,'username')->textInput(['autofocus'=>true]); ?><br/>
<?= $form->field($model,'password')->passwordInput(); ?><br/>
<?= $form->field($model,'repassword')->passwordInput(); ?><br/>
<?= $form->field($model,'email')->textInput(); ?><br/>
<?= $form->field($model,'sex')->inline()->radioList(['1'=>'男','2'=>'女']); ?><br/>
<?= $form->field($model,'verifyCode')->widget(Captcha::className(),['captchaAction'=>'index/captcha','imageOptions'=>['id'=>'captchaimg', 'title'=>'换一个', 'alt'=>'换一个', 'style'=>'cursor:pointer;'],'template'=>'{input}{image}']) ?><br/>
<div class="form-group">
<?= Html::submitButton('注册', ['class' => 'btn btn-primary', 'name' => 'signup-button']) ?>
</div>
<div class="form-group">
<?= Html::a('登录', ['index/login'], ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end();?>
- 2.控制器部分
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
'maxLength' => 4,
'minLength' => 3,
'padding' => 5,
'height' => 40,
'offset' => 4,
],
];
}
- 3.model部分
public function rules()
{
return [
['username', 'filter', 'filter' => 'trim'],
[['username','password','repassword','email','sex'], 'required'],
['username', 'unique', 'targetClass' => '\common\models\User', 'message' => '用户名已注册!'],
['username', 'string', 'min' => 6, 'max' => 16],
['username', 'match','pattern'=>'/^[(\x{4E00}-\x{9FA5})a-zA-Z]+[(\x{4E00}-\x{9FA5})a-zA-Z_\d]*$/u','message'=>'用户名由字母,汉字,数字,下划线组成,且不能以数字和下划线开头。'],
['email', 'filter', 'filter' => 'trim'],
['email', 'email'],
['email', 'string', 'max' => 255],
['email', 'unique', 'targetClass' => '\common\models\User', 'message' => '邮箱已注册!'],
['sex','double','min'=>'1','max'=>'2','message'=>'性别不合法!'],
[['password','repassword'], 'string', 'min' => 6 , 'max' => 18 , 'message' => '密码长度位6-18位!'],
['repassword','compare','compareAttribute' => 'password','message'=>'两次输入的密码不一致!'],
['verifyCode','captcha','captchaAction'=>'index/captcha']
共 1 个回答
-
cristalblock 回答于 2016-09-03 22:25 举报
官网api有说明的:
Note that once CAPTCHA validation succeeds, a new CAPTCHA will be generated automatically. As a result, CAPTCHA validation should not be used in AJAX validation mode because it may fail the validation even if a user enters the same code as shown in the CAPTCHA image which is actually different from the latest CAPTCHA code.附链接:
http://www.yiichina.com/doc/api/2.0/yii-captcha-captchavalidator共 1 条回复
lk1069319346
注册时间:2016-04-08
最后登录:2016-07-19
在线时长:5小时39分
最后登录:2016-07-19
在线时长:5小时39分
- 粉丝0
- 金钱10
- 威望0
- 积分60