ajax validation not work for one activefield

after typing the username when someone regist the site,

i want to show him if the username has already exist.so i come up with the ajax validation.

but i try hard ,it just not work.

here is some parts of my code(rules are tested ok when not use ajax validation,so not showed here),

may someone help me.




//view:signup.php

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

                                 'fieldConfig' => [

                                       'template'=>'{input}{error}',

                                 ],

                                 'validateOnChange'=>true,

                                 'validateOnSubmit'=>true,

                                 'enableClientValidation'=>true,

                                 'enableAjaxValidation'=>true,

                                 'id' => 'form-signup',

]);?>


<?= $form->field($model,'username',['enableAjaxValidation'=>true,'validateOnBlur'=>true,'validateOnChange'=>true])?>


//controller:actionSignup.php

public function actionSignup(){

     $model = new SignupForm();

     if (isset($_POST['ajax']) && $_POST['ajax'] === 'form-signup'){

        Yii::info("ajax validate");[u][b]//there is no log[/b][/u]

        $model->validate($model,['username']);

        $this->layout=null;

        return $model->getErrors();

     }

     .......

}




edit:do i need to write ajax code in js myself ?

You can use this…

//view:signup.php




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

                         		'fieldConfig' => [

                               		'template'=>'{input}{error}',

                         		],

                         		'validateOnChange'=>true,

                         		'validateOnSubmit'=>true,

                         		'enableClientValidation'=>true,

                         		'enableAjaxValidation'=>true,

                         		'id' => 'form-signup',

]);?>


<?= $form->field($model, 'username')->textInput(['maxlength' => 15]) ?>



//controller:actionSignup.php





public function actionSignup(){


 	$model = new SignupForm();


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

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

                   	return ActiveForm::validate($model);

         	}

 	//   Yii::info("ajax validate");[u][b]//there is no log[/b][/u]

      //  $model->validate($model,['username']);


   //   	$this->layout=null;

   // 	return $model->getErrors();

 	.......

}



thx,but it just not work either…

do you know how to check the ajax code generated by activeform/field’s settings in signup.php?

check error log in firebug console…

nothing~~~ i am not sure if i take all the necessary step . first of all,do i need to write jquery ajax code myself to make an ajax request for some field? or has it all been done by setting field and form like above.

can show your model file rules code ?

PLEASE JUST SEE THE RULE OF USERNAME




<?php

namespace frontend\models;


use common\models\User;

use yii\base\Model;

use Yii;




/**

 * Signup form

 */

class SignupForm extends Model

{

    public $username;//JUST SEE THIS PLEASE

    public $email;

    public $mailcode;

    public $password;


    public $intend;

    public $agree;


    /**

     * @inheritdoc

     */

    public function rules()

    {


        return [

            ['username', 'filter', 'filter' => 'trim'],

            ['username', 'required'],

            ['username', 'unique', 'targetClass' => '\common\models\User'],

            ['username', 'string', 'min' => 6, 'max' => 20],


            ['email', 'filter', 'filter' => 'trim'],

            ['email', 'required'],

            ['email', 'email'],

            ['email', 'unique', 'targetClass' => '\common\models\User'],


            ['password', 'required'],

            ['password', 'string', 'min' => 6,'max' => 10],


        ];

    }


    public function attributeLabels()

    {

        return [

            'email' => 'aa',

            'username' => 'aa',

            'password' => 'aa',

            'mailcode'=>'aa',

            'intend' => 'aa',

            'agree' => '同意',

        ];

    }


    public function signup()

    {


        if ($this->validate()) {

            Yii::info("attr is ".var_dump($this->attributes));

            return User::create($this->attributes);

        }


        return null;

    }

}

  



your model file code is correct; so now try this code

_form.php





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

    'id' => 'signup-form',

    'enableAjaxValidation' => true,

    'options' => ['class' => 'form-horizontal'],

]); ?>

<?= $form->field($model, 'username',['template' => "{label} {input}{error}"])->textInput(['maxlength' => 80]) ?>


<div class="form-group">

        <?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>

	<?= Html::a('Cancel', ['index'], ['class' => 'btn btn-default']) ?>

    </div>


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

controller.php





public function actionSignup(){


     $model = new SignupForm();


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


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

                       return ActiveForm::validate($model);

         }

................

...

}

thanks…

_form.php can only show username is already exist after button is clicked,just like how my code goes.

but i want to show existing message after username is typed and blured…

by the way,do you know how to let lable,input ,error layout in oneline just from left to right?