CActiveForm (afterValidate) problem in widget

Hello

I found ajax login code in this page:

this code working perfect in site controller - views/login

I see in output the js code:




<script type="text/javascript">

/*<![CDATA[*/

jQuery(function($) {

jQuery('#login-form').yiiactiveform({'validateOnSubmit':true,'afterValidate':function(form, data, hasError) {

...

</script>



but this code in widget:

no see this script in html output…




<script type="text/javascript">

/*<![CDATA[*/

jQuery(function($) {

jQuery('#login-form').yiiactiveform({'validateOnSubmit':true,'afterValidate':function(form, data, hasError) {

...

</script>



my code:

components/LoginWidget.php


class LoginWidget extends CWidget {


    private $_model;


    public function init() {

        $this->_model = new LoginForm;


        // if it is ajax validation request

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

            $errors = CActiveForm::validate($this->_model);

            if ($errors != '[]') {

                echo $errors;

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

            }

        }


        // collect user input data

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

            $this->_model->attributes = $_POST['LoginForm'];

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

            if ($this->_model->validate() && $this->_model->login()) {

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

                    echo CJSON::encode(array(

                        'authenticated' => true,

                        'redirectUrl' => Yii::app()->user->returnUrl,

                    ));

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

                }

                $this->controller->redirect(Yii::app()->user->returnUrl);

            }

        }

    }


    public function run() {

        $this->render('login', array('model' => $this->_model));

    }


}

components/views/login.php


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

                'id' => 'login-form',               

                'enableClientValidation' => true,                                

                'clientOptions' => array(

                    'validateOnSubmit' => true,

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

                if (!hasError){ 

                    str = $("#login-form").serialize() + "&ajax=login-form";

 

                    $.ajax({

                        type: "POST",

                        url: "' . Yii::app()->createUrl('/') . '",

                        data: str,

                        dataType: "json",

                        beforeSend : function() {

                            $("#login").attr("disabled",true);

                        },

                        success: function(data, status) {

                            if(data.authenticated)

                            {

                                window.location = data.redirectUrl;

                            }

                            else

                            {

                                $.each(data, function(key, value) {

                                    var div = "#"+key+"_em_";

                                    $(div).text(value);

                                    $(div).show();

                                });

                                $("#login").attr("disabled",false);

                            }

                        },

                    });

                    return false;

                }

            }',

                ),

            ));

            ?>

            <div class="modal-footer">

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

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

                <?php echo CHtml::submitButton('Bejelentkezés', array('id' => 'login', 'class' => 'btn btn-default')); ?>

            </div>

            <?php $this->endWidget(); ?>

this widget use in layout file:




<?php $this->widget('loginWidget'); ?>



Sorry bad English.