2016-05-23 171 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的組合。所以你可能會有一個錯誤配置的會話。或者您的驗證碼的操作名稱不是'驗證碼', - 您在該問題中遺漏了部分源代碼。