How to handle CSRF Validation in Yii2 Framework?

前端 未结 2 426
隐瞒了意图╮
隐瞒了意图╮ 2020-12-14 17:51

I\'m having problem with CSRF Validation in yii2. The validation works fine with the default form generated by the gii but when I edit the form with html tags then the form

相关标签:
2条回答
  • 2020-12-14 18:19

    I guess, your html form doesn't have hidden _csrf field, which is automatically generated by standard Yii2 widgets.

    So the minimum code of your custom form might be like this:

    <form method="post">
        <input type="hidden" name="<?= Yii::$app->request->csrfParam; ?>" value="<?= Yii::$app->request->csrfToken; ?>" />
        <button type="submit"> Save </button>
    </form>
    
    0 讨论(0)
  • 2020-12-14 18:23

    Try this

    <?=yii\helpers\Html::hiddenInput(Yii::$app->request->csrfParam, Yii::$app->request->csrfToken)?>
    
    0 讨论(0)
提交回复
热议问题