Search code examples
phptwitter-bootstrapyiiyii-extensions

Failed to initialize widget model is not set in yii


I'm getting error when i creating a Modal Login widget using Yii-Bootstrap extension.

CException Error - TbInput: Failed to initialize widget! Model is not set.

What is the wrong???

Here is my source code -

Login Button code -

in /views/layouts/main.php

<?php if (Yii::app()->user->isGuest): ?>
        $this->widget('bootstrap.widgets.TbButton', array(
            'size' => 'large',
            'label' => 'login',
            'url' => '#login-modal',
            'htmlOptions' => array(
              'data-toggle' => 'modal',
              'onclick' => '$("#error-div").hide();$("#LoginForm_username").focus();'),
            )
         );

         endif;

      $this->renderPartial("_ajax_login_form");
    ?>


ajax_login_form.php

<?php $this->beginWidget('bootstrap.widgets.TbModal', array(
        'id' => 'login-modal'
          )
      ); ?>
    <div class="modal-header">
        <a class="close" data-dismiss="modal">&times;</a>
        <h3>login</h3>
    </div>
    <div class="modal-body">
    <?php
        $form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
            'id' => 'LoginForm',
            'htmlOptions' => array(
                'class' => 'well'),
            )
         );
?>
    <div id="error-div" class="alert alert-block alert-error" style="display:none;">
    </div>    
    <?php echo $form->textFieldRow($login_form_model, 'username', array(
                            'class' => 'span3'
                            )
                      ); ?>
    <?php echo $form->passwordFieldRow($login_form_model, 'password', array(
                             'class' => 'span3'
                             )
                      ); ?>
    <?php $this->widget('bootstrap.widgets.TbButton', array(
            'buttonType' => 'ajaxSubmit', 
            'icon' => 'ok', 
            'label' => 'Submit', 
            'ajaxOptions' => array(
            'success' => 
      'function(data){
        var obj = $.parseJSON(data);
        if(obj.login=="success"){
            $("#login-modal").modal("hide");
            setTimeout(function(){location.reload(true);},400);
        }else{
            $("#error-div").show();
            $("#error-div").html("");
            if("LoginForm_password" in obj){
                $("#error-div").html(obj.LoginForm_password[0]+"<br />");
            }
            if("LoginForm_username" in obj){
                $("#error-div").append(obj.LoginForm_username[0]);
            }
        }
    }'),
));

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


SiteController.php

public function actionLogin(){

  //$this->layout = 'main';
  $model = new LoginForm();

  if(isset($_POST['ajax']) && $_POST['ajax'] === 'login-form')
    {
      echo CActiveForm::validate($model, array('username', 'password', 'verifyCode'));
      Yii::app()->end();
    }

  if (isset($_POST['LoginForm'])) {
        $login_form_model->attributes = $_POST['LoginForm'];
        if ($login_form_model->validate() and $login_form_model->login()) {
            $array = array('login' => 'success');
            Yii::app()->user->setFlash('success', 'Successfully logged in.');
            $json = json_encode($array);
            echo $json;
            Yii::app()->end();
        } else {
            echo CActiveForm::validate($login_form_model);
            Yii::app()->end();
        }
    }

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


login.php

<?php 

 $form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
      'id' => 'login-form',
      'enableClientValidation' => true,
      'htmlOptions' => array('class' => 'well'),
      'clientOptions' => array(
            'validateOnSubmit' => true,
        ),
     )); ?>
<?php echo $form->textFieldRow($model, 'username', array('class' => 'span3'));?>
<?php echo $form->passwordFieldRow($model, 'password', array('class' => 'span3'));?> 
<?php echo $form->checkBoxRow($model, 'rememberMe');?>
<?php $this->widget('bootstrap.widgets.TbButton', array('buttonType' => 'submit', 'type' => 'primary', 'label' => 'Submit', 'icon' => 'ok'));?>
<?php $this->widget('bootstrap.widgets.TbButton', array('buttonType' => 'reset', 'label' => 'Reset'));?>
 <?php $this->endWidget(); ?>

Please help me to resolve my error.


Solution

  • The error is generated because you don't pass the $model correctly to the view.

    Change you actionLogin() in your SiteController.php to

    public function actionLogin(){
    
     //$this->layout = 'main';
     $model = new LoginForm();
    
     if(isset($_POST['ajax']) && $_POST['ajax'] === 'login-form')
     {
        echo CActiveForm::validate($model, array('email', 'password', 'verifyCode'));
        Yii::app()->end();
     }
    
     if (isset($_POST['LoginForm'])) {
        $model->attributes = $_POST['LoginForm'];
        if ($model->validate() and $model->login()) {
            $array = array('login' => 'success');
            Yii::app()->user->setFlash('success', 'Successfully logged in.');
            $json = json_encode($array);
            echo $json;
            Yii::app()->end();
        } else {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
     }
    
     $this->render('login', array(
        'model' => $model,
      ));
    }
    

    Changes needed in your main.php file

     $login_form_model = new LoginForm();
     $this->renderPartial("_ajax_login_form",  array( 
                            'login_form_model' => $login_form_model,
                             )
                         );