Noobs To be Pro Addict
Noobs To be Pro Addict

Reputation: 395

Delete is not working Method Not Allowed (#405) gridview yii2

This is my gridview, I changed from class actionColumn to this:

[   'format' => 'html',
            'contentOptions'=>['style'=>'width: 5px;'],
            'value' => function($model) {
                if($model->id == Yii::$app->user->identity->id) {
                    return  Html::a('<i class="glyphicon glyphicon-share-alt"></i>').' '. 
                            Html::a('<i class="glyphicon glyphicon-pencil"></i>', ['update', 'id' => $model->id]).' '.
                            Html::a('<i class="glyphicon glyphicon-trash"></i>', ['delete', 'id' => $model->id], ['data' => ['confirm' => 'Do you really want to delete this element?','method' => 'post']]);
                }
                return '';
            },
        ],

Which gives me an error.

Method Not Allowed (#405)

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

When I changed to actionColumn again it was working, but I changed the code and it just gives me an error.

Upvotes: 5

Views: 3459

Answers (2)

BHoft
BHoft

Reputation: 1663

what you can also do is this set the buttons paramter in the actioncoloumn see http://www.yiiframework.com/doc-2.0/yii-grid-actioncolumn.html#$buttons-detail

e.g. something like this:

      'buttons' => [
            'update' => function ($url, $model, $key) {
                if ($model->id == Yii::$app->user->identity->id) {
                    $options = [
                    'title' => Yii::t('yii', 'Update'),
                    'aria-label' => Yii::t('yii', 'Update'),
                    'data-pjax' => '0',
                    ];
                    return Html::a('<span class="glyphicon glyphicon-pencil"></span>', $url, $options);
                }
            },
            'delete' => function ($url, $model, $key) {
                if ($model->id == 6929) {
                    $options = [
                    'title' => Yii::t('yii', 'Delete'),
                    'aria-label' => Yii::t('yii', 'Delete'),
                    'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'),
                    'data-method' => 'post',
                    'data-pjax' => '0',
                    ];
                    return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, $options);
                }
            },
        ],

But i would just extend my own class from the ActionColumn Class create a function like this. I understand your code that all buttons should be hidden or displayed depending if the model->id is the user->identity->id

protected function renderDataCellContent($model, $key, $index)
{
    if ($model->id == Yii::$app->user->identity->id) {
        return parent::renderDataCellContent($model, $key, $index);
    }
}

Hope this helps. I Would use the approach with the extended Actioncolumn class. Because then all links are still working if e.g. the urlCreator function is changed or pjax is enabled for the grid etc.

Nonetheless the reason why the post request wasn't working was correct as soju has written above.

Upvotes: 2

soju
soju

Reputation: 25312

Since the html formatter will clean the value using HtmlPurifier, you should simply change the format to raw .

Read more : http://www.yiiframework.com/doc-2.0/guide-output-formatter.html#other-formatters

Upvotes: 4

Related Questions