Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

yii2 - How to solve Bad Request (#400) Unable to verify your data submission?

I have a controller generated by Gii. I modify the behaviors to be like this:

public function behaviors()
{
    return [
        'verbs' => [
            'class' => VerbFilter::className(),
            'actions' => [
                'delete' => ['post'],
            ],
        ],
        'access' => [
            'class' => AccessControl::className(),
            'rules' => [
                [
                    'allow' => true,
                    'roles' => ['@'],
                ],
            ],
        ],
    ];
}

When I fill the form and submit it, sometimes I get an error

Bad Request (#400) Unable to verify your data submission

But if I click on back button in my browser and re-submit again the form with the same value it will be okay an submitted successfully.

I have been searching in google and stackoverflow, many of them say that the problem is on CSRF Token. But in my layout I've put <?= Html::csrfMetaTags() ?> and in my form there is <input type="hidden" name="_csrf" value="...">

Anyone here can help me to solve this? And explain this why does it happen?

like image 320
Wilianto Indrawan Avatar asked Jul 25 '15 03:07

Wilianto Indrawan


1 Answers

add in form in view

<input type="hidden" name="_csrf" value="<?=Yii::$app->request->getCsrfToken()?>" />
like image 137
Knase Avatar answered Sep 30 '22 16:09

Knase