Yii Framework Forum: CActiveForm (afterValidate) problem in widget - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

CActiveForm (afterValidate) problem in widget Rate Topic: -----

#1 User is offline   sniper888 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 20-November 13

Posted 11 August 2015 - 12:34 PM

Hello

I found ajax login code in this page:
tahiryasin.wordpress.com/2013/05/23/ajax-based-yii-login-form/

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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users