Answer the question
In order to leave comments, you need to log in
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>
public function actionIndex()
{
if (\Yii::$app->user->isGuest) {
return $this->render('login');
}
else {
return $this->goBack();
}
}
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');
}
}
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;
}
}
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.
Answer the question
In order to leave comments, you need to log in
<input type="hidden" name="_csrf" value="<?=Yii::$app->request->getCsrfToken()?>" />
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()?>" />
In the layout, in the head section, insert <?= Html::csrfMetaTags() ?>
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 questionAsk a Question
731 491 924 answers to any question