yii2删除GridView时:无法验证您的数据提交

时间:2016-10-04 14:39:03

标签: gridview yii2 delete-row behavior

当我想删除GridView上的项目时,我收到此错误:

exception 'yii\web\BadRequestHttpException' with message 'Unable to verify your data submission.

我的这个我的控制器代码:

class DevisController extends Controller
{
 public $layout = 'lay-admin';

 public function behaviors()
 {
    return [
        'verbs' => [
            'class' => VerbFilter::className(),
            'actions' => [
                'delete' => ['post'],
            ],
        ],
    ];
}
/* ..... */
public function actionDelete($id)
{
    $this->findModel($id)->delete();

    return $this->redirect(['index']);
}

当我在行为函数中将post方法更改为get方法时,我收到此错误

Method Not Allowed. This url can only handle the following request methods: GET.

GridView代码:

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
                'columns' => [
                    ['class' => 'yii\grid\SerialColumn'],

                    //'idDevis',
                    'reference',
                    'client',
                    'dateCreation',
                    'contact',
                    'valableJusqua',
                    'dateRelance',
                    [
                     'attribute'=>'etat',
                     'filter'=>ArrayHelper::map(Devis::find()->asArray()->all(), 'etat', 'etat'),
                    ],
                    'commercial',
                    'modePaiement',
                    'delaiPaiement',

                    ['class' => 'yii\grid\ActionColumn'],
                ],
]); ?>

任何想法请!!

1 个答案:

答案 0 :(得分:2)

在自定义布局文件中添加CSRF元标记。

示例:

<?php $this->beginPage() ?>
<!DOCTYPE html>
<html lang="<?= Yii::$app->language ?>">
<head>
    <meta charset="<?= Yii::$app->charset ?>">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <?= Html::csrfMetaTags() ?>

    <title><?= Html::encode($this->title) ?></title>
    <?php $this->head() ?>
</head>
<body>
<?php $this->beginBody() ?>

Here you can read more about CSRF