Search code examples
gridviewyii2delete-rowbehavior

yii2 When delete GridView : Unable to verify your data submission


When I want to delete an item on GridView I get this error:

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

My this my controller code:

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']);
}

And when I change the post method to the get method in behaviors function I get this error

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

GridView code:

<?= 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'],
                ],
]); ?>

any idea please!!


Solution

  • Add CSRF meta tag in your custom layout file.

    Example:

    <?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.