2016-05-23 185 views
-1

提示:用户名和电子邮件是由ajaxValidation与验证方法检查。captcha do not not valid in yii2

所有是正确的,但我猜captcha是在服务器中更改,但旧图片在客户端。

我googling很多没有结果发现。

这是视图:

<?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'], 
    ]; 
} 

存在控制器无行为

回答

0
  1. 形式的enableAjaxValidation优先于一个领域,所以很难说正是你正在尝试与您的当前设置做
  2. 确保你将verifyCode声明为模型中的属性
  3. 验证码验证码保存在会话中。会话密钥是控制器和操作ID的组合。所以你可能会有一个错误配置的会话。或者您的验证码的操作名称不是'验证码', - 您在该问题中遗漏了部分源代码。