Yii Ajax Validation in widget not working

I’m using Ajax validation in my widget. Tried this for hours but it’s not working still. Please help!!

Widget function


public function run(){


if(!Yii::app()->user->isGuest){

    $this->controller->redirect('/');

}


$model= new LoginForm;


// if it is ajax validation request

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

{

    echo CActiveForm::validate($model);

    Yii::app()->end();

}

    if(isset($_POST['LoginForm']))

    {


        $model->attributes=$_POST['LoginForm'];

        // validate user input and redirect to the previous page if valid

        // blah blah.......

Widget View:


<?php $form=$this->beginWidget('CActiveForm', array(

    'id'=>'login-form',

    'enableAjaxValidation'=> true,

    'enableClientValidation'=>true,

    'clientOptions'=>array(

        'validateOnSubmit'=>true,

    ),

)); ?>




    <div class="row">

        <?php echo $form->textField($model,'username',array('placeholder'=>'email','class'=>'form-control')); ?>

        <?php echo $form->error($model,'username'); ?>

    </div>


    <div class="row">

        <?php echo $form->passwordField($model,'password',array('placeholder'=>'password','class'=>'form-control')); ?>

        <?php echo $form->error($model,'password'); ?>

    </div>




    <div class="row buttons">

        <?php echo CHtml::submitButton('Login', array('class'=>'btn btn-primary btn-block')); ?>

    </div>

...

Right now the form is not submitting. After I click login nothing happens.

If I make enableAjaxValidation fale, form works but not AJAX.

If I make enableClientValidation false, form works but still no AJAX.

Any ideas? Thanks.

You have to use ‘afterValidate’ inside ‘clientOptions’. Use $.ajax() function to submit the data.




'afterValidate'=>'js:function(form,data,hasError){

                        if(!hasError){

                                $.ajax({

                                        "type":"POST",

                                        "url":"your url",

                                        "data":form.serialize(),

                                        "success":function(data){},

                                        "error":function(xhr, status, error){alert(xhr.responseText);},

                                        });

                                }

                        }'



Sorry but that does not work :(

Whether ajax validation is working? Did you check in the browser’s network console?

Yes. nothing happens there. form doesn’t get submitted.

Whether the form is available online? Can you post the link?