Yii 2.0 Error "Unable to verify your data submission"

In Yii 2.0 Framework Dev, when post data to controller will get this kind error

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

 

after analysis we found, it because form data not have csrf value,  Yii 2.0 enabled csrf check as default, so we can find the follow hidden input in normal Yii 2.0 form

<input type="hidden" name="_csrf" value="LmZwR0lqU2RbMy8pcEcmV2lRJgA9IRwTSSkScDg7YzEDLAkWBSM8Vg==">

 

the solution is very simple, just add the follow input into your form area

<input type="hidden" name="_csrf" value="<?= Yii::$app->request->csrfToken; ?>">

 

To remind you Yii::$app->request->csrfToken will direct return the csrfToken for Yii csrf check

 

{ Other Solution }

1. Disable csrf check on given action - Not Suggest!! Only for special condition

add the follow code to the action part, will disable csrf check

Yii::$app->controller->enableCsrfValidation = false;

 

2. Disable csrf check on one Controller's all Actions - Not Suggest!! Only for special condition

add the follow code to controller part

public $enableCsrfValidation = false;

 

{ Lee Say }

Try keep CSRF check enable in Yii 2.0 Framework projects, it's useful for all projects.