How can i show the default login page as modal popup in yii2?

狂风中的少年 提交于 2019-12-22 11:29:51

问题


Can anybody please tell me how can i raise the default login page as model popup in yii2?

Thanks


回答1:


Login link

<a href="javascript:void(0);" onclick="login();return false;">Login</a>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"></div>
<script>
   function login(){
      $.ajax({
          type:'POST',
          url:'index.php?r=site/login',
          success: function(data)
                   {
                       $('#myModal').html(data);
                       $('#myModal').modal();
                   }
      });
   }
</script>

In Controller (Site controller/login action)

return $this->renderAjax('_login', [
    'model' => $model,
]);

You have to create a partial file

Partial view _login.php

<?php 
    use yii\helpers\Html;
    use yii\widgets\ActiveForm;
?>
<!-- modal dialog for display pop up login -->
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" ><span aria-hidden="true">&    times;</span><span class="sr-only">Close</span></button>
    <h4 class="modal-title" id="myModalLabel">Login</h4>
</div>
<div class="modal-body">
    <!-- start ActiveForm -->
    <?php $form = ActiveForm::begin(['id' => 'login-form','enableClientValidation'=>'true']); ?>

    <?= $form->field($model, 'username',[
                                  'template' => "{label}\n{error}\n{input}\n{hint}\n",
                                  'errorOptions'=>['class'=>'help-block alert alert-danger','style'=>'display:none;']
                             ]
                    ) ?>
    <?= Html::a('Forgot your password?', ['site/repassword'],['class'=>'password-recovery']) ?>
    <?= $form->field($model, 'password',[
                   'template' => "{label}\n{error}\n{input}\n{hint}\n",
                   'errorOptions'=>['class'=>'help-block alert alert-danger','style'=>'display:none;']
                ])->passwordInput() ?>
   <div class="modal-footer">
   <div class="form-group our-form-group">
         <?= Html::a('Sign up', ['site/signup']) ?>
         <?= Html::submitButton('Login', ['class' => 'btn btn-primary', 'name' => 'login-button']) ?>
   </div>
   <?php ActiveForm::end(); ?>
</div>
</div>
</div>




回答2:


http://getbootstrap.com/javascript/#modals-usage

<a data-toggle="modal" href="<?= Url::toRoute(Yii::$app->user->loginUrl) ?>" data-target="#modal">Login</a>

and in action use renderPartial()



来源:https://stackoverflow.com/questions/23927579/how-can-i-show-the-default-login-page-as-modal-popup-in-yii2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!