P
P
Pavel Ivanov2014-09-17 09:37:04
Yii
Pavel Ivanov, 2014-09-17 09:37:04

Why does Bad Request (#400) occur on a form in yii 2.0?

There is a form:

<div data-role="page" id="login">
    <div data-role="header">
        <h2>Авторизация</h2>
    </div>
    <div role="main" class="ui-content jqm-content">
        <form role="form" action="/site/login" method="POST" id="login-form">
            <div class="ui-grid-solo">
                <div class="ui-grid-a">
                    <label for="username">Имя</label>
                    <input type="text" name="username" id="username" value="" data-clear-btn="true" data-mini="true">
                    <label for="password">Пароль</label>
                    <input type="password" name="password" id="password" value="" data-clear-btn="true" autocomplete="off" data-mini="true">

                    <input type="checkbox" name="remind" id="remind" value="1">
                    <label for="remind">Запомнить меня</label>
                    <br>
                    <input type="submit" value="Логин" onclick="this.form.submit();">
                </div>
            </div>
        </form>
    </div>
</div>

In SiteController I redirect to the form:
public function actionIndex()
    {

        if (\Yii::$app->user->isGuest) {
            return $this->render('login');
        }
        else {
            return $this->goBack();

        }
    }

In the same SiteController, actionLogin is being processed:
public function actionLogin()
    {
        if (!\Yii::$app->user->isGuest) {
            return $this->render('index');
        }

        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->render('about');
        } else {
            return $this->render('contact');
        }
    }

In LoginForm:
public function rules()
    {
        return [
            [['username', 'password'], 'required'],
            ['remind', 'boolean'],
            ['password', 'validatePassword'],
        ];
    }

public function login()
    {
        if ($this->validate()) {
            return Yii::$app->user->login($this->getUser(), $this->remind ? 3600 * 24 * 30 : 0);
        } else {
            return false;
        }
    }

When sending a POST message, an error occurs:
Bad Request (#400)
Unable to verify your data submission.
The above error occurred while the Web server was processing your request.
Please contact us if you think this is a server error. thank you.

What's the matter?

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
M
mrlipuzhin, 2015-06-11
@mrlipuzhin

<input type="hidden" name="_csrf" value="<?=Yii::$app->request->getCsrfToken()?>" />

N
nepster-web, 2014-09-17
@nepster-web

Remind me why you use the usual layout, and not Yii2 features?
you can disable the token check
https://github.com/yiisoft/yii2/blob/master/framew...
In your controller
The second option is to create an input in which you specify the token as value:
https://github.com/yiisoft/ yii2/blob/master/framew...

<input type="hidden" value="<?=Yii::$app->request->getCsrfToken()?>" />

D
developinwed, 2014-10-26
@developinwed

In the layout, in the head section, insert <?= Html::csrfMetaTags() ?>

A
Alexander Maltsev, 2015-10-02
@alexandro5

Hello!
Guys, well, I have a token, what else could be the problem?
in the layout
on the local everything works (Open Server)
on the server after loading it works exactly for about 2 days then, an error + in the backend there was such a picture
of PHP on the server
Version Yii 2.0.4

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question