2017-08-19 13 views
0

私のフォームに書かれたフォームにアクティブなAjaxの検証を追加しようとしています( 'enableAjaxValidation' => trueとvalidationUrl)そのアクションで 検証は実行されません私のフォーム? iはyii2フォーム入力ajaxの検証は実行されません

これは私のモデルのルールが機能ここ

<div class="developer-form"> 

    <?php $form = ActiveForm::begin([ 
     'id' => $model->formName(), 
     'enableAjaxValidation' => true, 
     'validationUrl'=>\yii\helpers\Url::toRoute('site-admin/validation') 
    ]); ?> 

    <?= $form->field($model, 'name')->textInput(['minlength'=>true]) ?> 
    <?= $form->field($model, 'family')->textInput() ?> 
    <?= $form->field($model, 'phone')->textInput() ?> 
    <?= $form->field($model, 'email')->textInput() ?> 
    <?= $form->field($model, 'address')->textInput() ?> 
    <?= $form->field($model, 'brithday')->textInput() ?> 
    <?= $form->field($model, 'age')->textInput() ?> 
    <?= $form->field($model, 'ability')->textInput() ?> 
    <?= $form->field($model, 'role')->textInput() ?> 
    <?= $form->field($model, 'point')->textInput() ?> 
    <?= $form->field($model, 'join_date')->textInput() ?> 

    <div class="form-group"> 
     <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> 
    </div> 

    <?php ActiveForm::end(); ?> 
</div> 

と私のコントローラ

public function actionCreate() 
{ 

    $model = new Developers(); 

     return $this->render('_form', [ 
      'model' => $model 
     ]); 

} 


public function actionValidation(){ 

    $model = new Developers(); 
    if(Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) 
    { 
     Yii::$app->response->format = 'json'; 
     return ActiveForm::validate($model); 
    } 
} 

とその私の形である

ありがとうコンソール内の任意のエラーを持っていけない

public function rules() 
{ 
    return [ 
     [['name', 
      'family', 
      'phone', 
      'email', 
      'address', 
      'brithday', 
      'age', 
      'ability', 
      'role', 
      'point', 
      'join_date', 
      ], 'required'], 
     [['id'], 'integer'], 
     [['date_project_done','estimate_for_next_project','number_of_project','activity_rate','free_rate','project_done'], 'safe'], 
     [['address'], 'string', 'max' => 100], 
     [['name'], 'string', 'min' => 3], 

    ]; 
} 
+0

は、ブラウザのコンソールで確認することができますか? –

+0

@Fabrizio Caldarelli 検証アクションが呼び出されません。どうして ? – moh

+0

あなたのコードにjavascriptの構文エラーがあるかどうかチェックしましたか? –

答えて

0
ビューのファイル形式の変数でコードの下

用途:

<?php $form = ActiveForm::begin([ 
       'id' => 'login-form', //your form id 
       'enableAjaxValidation' => true, 
       'enableClientValidation' => false, 
       'validateOnBlur' => false, 
       'validateOnType' => false, 
       'validateOnChange' => false, 
      ]) ?> 

お使いのコントローラのアクション内のコード下記の使用:検証アクションが呼び出された場合

public function actionCreate() 
{ 
    $model = new Developers(); 
    if(Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) 
    { 
     if($model->validate()) 
     { 
     // do here anything 
     $model->save(); 
     } 
     else 
     { 
     Yii::$app->response->format = 'json'; 
     return ActiveForm::validate($model); 
     } 
    } 
    else 
    { 
    return $this->render('_form', [ 
     'model' => $model 
    ]); 
    } 
} 
関連する問題