I
I
Ivan Yakushenko2016-07-16 11:55:25
Yii
Ivan Yakushenko, 2016-07-16 11:55:25

Why is Yii2 authorization not working?

Version Yii2 basic 2.0.9. Standard authorization via LoginForm works. It is necessary to add the possibility of authorization through a modal window located in layouts/main.php. Next form:

<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="LoginForm">
              <div class="modal__input">
                <div class="modal__label">
                  Ваш логин:*
                </div>
                <div class="modal__input-block">
                  <input type="text" name="LoginForm[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="LoginForm[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>

When trying to authorize, the modal window closes, authorization fails.
In the debugger in Request $_POST holds
_csrf 'azZHQ0thSTFYcyw7E1IIATwEKRs7JD1QGHoNBw4uOXo8BgwkJA0AeA=='
LoginForm [
'username' => 'test'
'password' => 'test'
]
login-button ''

The logs are empty.
If you authorize through the standard form, then $_POST is nothing, but there are 5 info records in the logs about working with yii\web\Session::open, yii\db\Command::query and yii\db\Connection::open
Tried instead of
<input type="text" name="LoginForm[username]" class="input__item">

use what is used in login.php:
<?= $form->field($model, 'username')->textInput(['autofocus' => true]) ?>

But then the page doesn't load, instead an error
PHP Fatal Error - yii\base\ErrorException
Call to a member function formName() on null

In theory, I understand that the LoginForm is processed by the actionLogin controller, which renders the login page itself, but I don’t know how to force the modal window to transmit authorization data.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikita, 2016-07-16
@kshnkvn

ActiveForm::begin(['action' =>['site/login'], 'id' => 'login-form', 'method' => 'post',]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question