R
R
raycheel2014-04-20 21:02:56
Yii
raycheel, 2014-04-20 21:02:56

How not to refresh the page on login in a modal window in Yii?

Hi all! Created an ajaxLink that loads the view (login form) into the modal window, when the data is correct, I immediately log in, but when the data is not correct, it takes me to the page with the view (it is rendered via renderPartial), how to make it so that errors were displayed in a modal window and I was not transferred to another page? Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
raycheel, 2014-04-21
@raycheel

@0neS

// Контроллер Login
  public function actionLogin()
  {

    if (Yii::app()->user->isGuest) {
            if (isset($_POST['ajax']) && $_POST['ajax'] === 'UserLogin') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
      $model=new UserLogin;
      // collect user input data
      if(isset($_POST['UserLogin']))
      {
        $model->attributes=$_POST['UserLogin'];
        // validate user input and redirect to previous page if valid
        if($model->validate()) {
          $this->lastViset();
          if (Yii::app()->getBaseUrl()."/index.php" === Yii::app()->user->returnUrl)
            $this->redirect(Yii::app()->user->returnUrl);
          else
            $this->redirect('site/index');
        }
      }
      // display the login form
      $this->renderPartial('/user/login',array('model'=>$model));
    } else
      $this->redirect(Yii::app()->controller->module->returnUrl);
  }

// Вид
<?php $form=$this->beginWidget('CActiveForm', array(
        'id'=>'UserLogin',
        'enableAjaxValidation'=>true,
)); ?>
<h4>Вход</h4>
<hr>
<?php echo CHtml::errorSummary($model); ?>
  <div class="form-group">
    <div class="col-sm-12">
      <div class="input-group">
        <span class="input-group-addon"><i class="fa fa-user"></i></span>
        <?php echo $form->textField($model,'username', array('placeholder'=>'Логин', 'class'=>'form-control')) ?>
      </div>
    </div>
  </div>
  <div class="form-group">
    <div class="col-sm-12">
      <div class="input-group">
        <span class="input-group-addon"><i class="fa fa-lock"></i></span>
          <?php echo $form->passwordField($model,'password', array('placeholder'=>'Пароль', 'class'=>'form-control')) ?>
      </div>
    </div>
  </div>
              
  <?php echo CHtml::submitButton('Вход', array('class'=>'btn btn-primary')); ?>
  <a href="<?php echo Yii::app()->createUrl('user/registration'); ?>" class="btn btn-default" data-dismiss="modal" type="button">Регистрация</a>

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

C
Cage, 2014-04-21
@Cage

use CHtml::ajaxSubmitButton instead of CHtml::submitButton
read more here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question