My ajax validation of one filed in a form not work

my aciton is




        public function actionHello()

        {

                Yii::info("come into ajax hello####");

                $model = new SignupForm(['scenario' => 'hello']);


                if (Yii::$app->request->isAjax ){

                        Yii::info("this is ajax ####");//Never COMES HERE!

                        if($model->load(Yii::$app->request->post())) {

                        Yii::$app->response->format = Response::FORMAT_JSON;

                        return ActiveForm::validate($model);

                        }

                }else{

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

                }

        }



my form is also simple:





<?php

use yii\helpers\Html;

use yii\widgets\ActiveForm;

use common\includes\CommonUtility;


$this->title = 'Signup';

$this->params['breadcrumbs'][] = $this->title;

?>


<div class="sg-bg">

        <div  class="sg-outer">

                <?php $form = ActiveForm::begin([

                                                        'enableAjaxValidation'=>true,

                                                        'id' => 'form-sgup',

                             ]);?>

                                <div class="sg">

                                        <div class="sgr"><?= $form->field($model,'username')?></div>

                                        <div class="sgl">昵称</div>

                                </div>

                                <div class="sg">

                                        <div class="sgr"><?= $form->field($model, 'email')?></div>

                                        <div class="sgl">邮箱</div>

                                                                <div class="sg">

                                        <div class="sgr">

                                        <?= Html::submitButton('测试', ['class' => 'sbtn btn btn-primary']) ?>

                                        </div>

                                </div>

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

        </div>

</div>



why the ajax request never triggered when fields losing focus(as commented in the code)?

I have referenced a usefull links :


http://stackoverflow.com/questions/28954523/yii2-ajax-form-validation-on-an-ajax-submitted-form

I think i didnt miss necessary codes in the project.

Also,/frontend/web/assets/61f38892/yii.validation.js is loaded when checking debug info in chrome.

OR

Can it be that there’s global settings that prevent the yii generated ajax from calling the validation?

I had the same issue and after a few hours of searching the Internet and the Yii Forum, I managed to solve it (thanks to the recommendation to check the AJAX response using Firebug on this page):

in your action the code should be (or use alias at the beginning of your controller)




        public function actionHello()

        {

                Yii::info("come into ajax hello####");

                $model = new SignupForm(['scenario' => 'hello']);


                if (Yii::$app->request->isAjax ){

                        Yii::info("this is ajax ####");//Never COMES HERE!

                        if($model->load(Yii::$app->request->post())) {

                        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

                        return \yii\widgets\ActiveForm::validate($model);

                        }

                }else{

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

                }

        }

This guide (http://www.yiiframew…validation.html) should be updated to reflect the above.