2016-10-04 45 views
1

當我想刪除GridView的一個項目我得到這個錯誤:yii2當刪除的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'], 
       ], 
]); ?> 

任何想法請!

+0

您的視圖中是否存在CSRF參數? – Bizley

+0

我不認爲,我看不到這個參數 –

+0

現在我得到這個錯誤'方法不允許。這個URL只能處理下面的請求方法:POST.' –

回答

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

+0

它的工作,謝謝 –

+0

強大的答案,搜索了很多地方。 – Bira