I am trying to make a login form using active forms in yii2 framework. It works fine and take the user to the dashboard. But, after clearing the history and all from my browser, it says "Bad Request, Unable to Submit Your Form Data". And in the second attempt, It logs me in. What could be the problem here. This is my controller .
public function actionIndex(){
$model = new LoginForm();
$Session= Yii::$app->session;
if (!Yii::$app->user->isGuest) {
return $this->goHome();
}
if($model->load(Yii::$app->request->post()) && $model->login()){
$Data = Yii::$app->request->post();
$username = $Data['LoginForm']['username'];
$adminDetail = RegisterForm::find()
->where(['username'=>$username])
->all();
$id= $adminDetail[0]['id'];
$adminName= $adminDetail[0]['name'];
$username= $adminDetail[0]['username'];
$phone= $adminDetail[0]['phone'];
$email= $adminDetail[0]['email'];
$lastName= $adminDetail[0]['lastName'];
$bio= $adminDetail[0]['bio'];
$city= $adminDetail[0]['city'];
$dob= $adminDetail[0]['date_of_birth'];
$status= $adminDetail[0]['status'];
$Session->set('id', $id);
$Session->set('id', $id);
$Session->set('username', $username);
if($status==0){
Yii::$app->user->logout();
Yii::$app->getSession()->setFlash('Disallow', 'Your membership has been expired.</n> <a href="#">Please Click Here To Upgrade</a>');
$this->redirect(array('/'));
}
return $this->redirect(['/dashboard']);
}
if($Session->get('id')){
$find= RegisterForm::find()
->where(['id'=>$Session->get('id')])
->all();
$adminName= $find[0]['name'];
return $this->render('adminLogin',[
'model'=>$model,
'Session'=> $Session,
'adminName'=>$adminName,
]);
}
else{
return $this->render('adminLogin',[
'model'=>$model,
'Session'=> $Session,
]);
}
}
This is my view File
<?php $form = ActiveForm::begin([
'id' => 'login-form login-username login-password login-remember-me',
'layout' => 'horizontal',
'options' => ['class' =>['form-horizontal push-30-t push-50
'],
],
]); ?>
<?= $form->field($model, 'username')->textInput(['autofocus' => true]) ?>
<?= $form->field($model, 'password')->passwordInput() ?>
<?= $form->field($model, 'rememberMe')->checkbox([
]) ?>
<div class="form-group">
<div class="col-lg-offset-1 col-lg-11">
<?= Html::submitButton('Login', ['class' => 'btn btn-primary', 'name' => 'login-button']) ?>
</div>
</div>
This happens mostly due to csrf
validation.
First, try to verify if it is happening on every form on your site. Verify by using default contact us form provided by yii framework try to submit it and see if it happens with that form too.
Then check that the layout file you are using contains the line
<?= Html::csrfMetaTags () ?>
If not, update your main layout file by adding this line in the <head>
section. This change is needed because yii\\web\\View
no longer automatically generates CSRF
meta tags due to issue #3358
.
If still cannot resolve remove cache and delete cookies from your browser and try again.
If still the same then you can turn off CSRF validation for the specific controller like below
public function init()
{
parent::init();
\Yii::$app->controller->enableCsrfValidation = false;
}
or for a specific action in beforeAction()
/**
* @inheritdoc
*/
public function beforeAction($action)
{
if ($action->id == 'my-method') {
$this->enableCsrfValidation = false;
}
return parent::beforeAction($action);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.