I
I
Ivan Yakushenko2016-07-15 17:54:03
Yii
Ivan Yakushenko, 2016-07-15 17:54:03

How to connect authorization to Yii2 modal window?

The template has the following modal window with an authorization form, I built elements from the standard login.php into views into it, connected all the dependencies:

<div class="modal fade" id="enter" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="site-login">
<?php $form = ActiveForm::begin([
        'id' => 'login-form',
      ]); ?>
  <div class="modal__dialog" role="document">
    <div class="modal__content">
      <div class="modal__header">
        <button type="button" class="modal__close" data-dismiss="modal" aria-label="Close">
          <img src="images/modal-close.png" alt="">
        </button>
        <h4 class="modal__title">Войти на сайт</h4>
      </div>
      <div class="modal__body">
          <div class="enter-form">  
            <form action="">
              <div class="modal__input">
                <div class="modal__label">
                  Ваш логин:*
                </div>
                <div class="modal__input-block">
                  <input type="text" name="username" class="input__item">
                  <a href="#" class="ques-icon">
                    <img src="images/ques-icon.png" alt="">
                  </a>
                </div>
              </div>
              <div class="modal__input">
                <div class="modal__label">
                  Пароль:*
                </div>
                <div class="modal__input-block">
                  <input type="password" name="password" class="input__item input__item_password">
                  <a href="#" class="forget-link">Забыли пароль?</a>
                </div>
              </div>
              <div class="submit__input">
                <?= Html::submitButton('Войти', ['class' => 'input__item', 'name' => 'login-button']) ?>
              </div>
            </form>
          </div>
        </div>
      </div>
    </div>
  </div>
<?php ActiveForm::end(); ?>
</div>
</div>

But when the form is submitted, nothing happens, as I understand it, the server simply does not accept this information. After pressing the "Login" button, the page reloads, and in the debugger in the Request section in $_POST the following hangs:
username 'test'
password 'test'
login-button ''

There are no entries in the log about starting the session and working with yii\db\Connection, if you use the standard authorization page, then there is nothing in the Request section in $_POST, but there are 5 info entries in the logs about working with yii\web\Session::open , yii\db\Command::query yii\db\Connection::open, while authorization is successful and transfers to the personal account.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Kim, 2016-07-15
@kshnkvn

There is no difference - in the modal window, the authorization form, or on the page. The main thing is that the form itself sends the correct data to the right place.
If you are submitting a form to a controller action, then you must load it in that action for validation. Those. for authorization, you need to create a LoginForm model with two (at least name/email and password) fields. In the modal window, paste the correct code:

$form = ActiveForm::begin();
echo $form->field($model, 'email');
echo $form->field($model, 'password')->passwordInput();
ActiveForm::end();

Then $_POST will receive data in the following form:
LoginForm[email] = ''
LoginForm[password] = ''

And in the controller:
$model = new LoginForm();
if (Yii::$app->request->isPost){
  if ($model->load(Yii::$app->request->post()) && $model->validate()){
    ... далее авторизация
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question