验证码在yii2中验证

时间:2016-05-23 15:42:07

标签: php yii yii2 captcha yii2-advanced-app

提示:用ajaxValidation和validate方法检查用户名和电子邮件。

一切都是正确的,但我想在服务器中更改验证码,但旧图片在客户端。

我在谷歌上搜索没有找到任何结果。

这是观点:

<?php
    $form = \yii\widgets\ActiveForm::begin([
                'id' => 'form-signup',
                'action' => 'signup',
                'enableAjaxValidation' => false,
                'enableClientValidation' => true,
                'validationUrl' => 'validation',
                'validateOnBlur' => true,
                'fieldConfig' => [
                    'template' => '<div class="col-md-4" >{label}{input}{error}</div>'
                ]
    ]);
    ?>

    <?= $form->field($signup, 'username', ['enableAjaxValidation' => true]) ?>
    <?= $form->field($signup, 'name') ?>
    <?= $form->field($signup, 'family') ?>
    <?= $form->field($signup, 'mobile') ?>
    <?= $form->field($signup, 'password')->passwordInput() ?>
    <?= $form->field($signup, 'password_repeat')->passwordInput() ?>
    <?= $form->field($signup, 'email', ['enableAjaxValidation' => true]) ?>
    <?= $form->field($signup, 'verifyCode', ['enableAjaxValidation' => false])->widget(yii\captcha\Captcha::className()) ?>
    <div class="form-group">
        <?= yii\helpers\Html::submitButton('signup', ['class' => 'btn btn-green margin-right', 'name' => 'signup-button']) ?>

    </div>

控制器:

        $model = new SignupForm();
    if ($model->load(Yii::$app->request->post())) {
        if ($model->validate()) {
            if ($user = $model->signup()) {
                if (Yii::$app->getUser()->login($user)) {
                    return $this->goHome();
                }
            }
        } else {
    ;
            \yii\widgets\ActiveForm::validate($model);
        }
    } else {
        return $this->render('/partials/_signup', ['signup' => $model]);
    }

ajax验证控制器方法:

  public function actionValidation() {
    $model = new SignupForm();
    if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
        Yii::$app->response->format = 'json';
        return \yii\widgets\ActiveForm::validate($model);
    }
}

模型:

 public $name;
public $family;
public $mobile;
public $username;
public $email;
public $password;
public $password_repeat;
public $verifyCode;

   public function rules() {
    return [
        [['name', 'family', 'mobile'], 'default'],
        ['name', 'string', 'max' => 50],
        ['family', 'string', 'max' => 50],
        ['mobile', 'string', 'max' => 11],
        ['username', 'filter', 'filter' => 'trim'],
        ['username', 'required'],
        ['username', 'string', 'min' => 2, 'max' => 255],
        [['username'], 'unique', 'targetClass' => '\frontend\models\User', 'message' => 'username already taken.'],
        ['email', 'filter', 'filter' => 'trim'],
        ['email', 'required'],
        ['email', 'email'],
        ['email', 'string', 'max' => 255],
        ['email', 'unique', 'targetClass' => '\frontend\models\User', 'message' => 'email name already taken.'],
        ['password', 'required'],
        ['password', 'string', 'min' => 6, 'max' => 255],
        ['password_repeat', 'string', 'min' => 6, 'max' => 255],
        ['password_repeat', 'compare', 'compareAttribute' => 'password'],
        ['verifyCode', 'captcha'],
    ];
}

控制器中没有任何行为

1 个答案:

答案 0 :(得分:-1)

  1. 表单的enableAjaxValidation优先于字段的值,因此很难说明您尝试使用当前设置的确切内容
  2. 确保将verifyCode声明为模型上的属性
  3. 验证码验证码保存在会话中。会话密钥是控制器和操作ID的组合。所以你可能会有一个配置错误的会话。或者您的验证码的动作名称不是“验证码”, - 您在问题中缺少源代码的那部分。