Blackjack Blackjack - 3 years ago 233
PHP Question

Missing Required Parameter in Yii2 Gridview Action Button

I'm trying to add my own action button in Yii2-Kartik Gridview.
This is my custom button:
enter image description here

This is my code in index.php



[
'class' => 'yii\grid\ActionColumn',
'template' => '{edit}',
'buttons' => [
'edit' => function ($url, $model) {
return Html::a('<button type="button" class="btn btn-edit-npwp"><i class="glyphicon glyphicon-plus-sign"></i></button>', $url, [
'title' => Yii::t('app', 'Edit'),
'data-toggle' => "modal",
'data-target' => "#myModal",
'data-method' => 'post',
]);
},
],
'urlCreator' => function ($action, $model, $key, $index) {
if ($action === 'edit') {
$url = Url::toRoute(['vatout-faktur-out/add-data', 'id' => $model->fakturId], ['data-method' => 'post',]);
return $url;
}
}
],


and this is my action in controller.php



public function actionAddData($id) {
$model = new VatoutFakturOut();
return $this->render('addData', [
'model' => $model,
]);
}


I want to process the data from the row that I've clicked the button.
But, this return error


Missing required parameters: id


Why does this happen? And how to fix this?



Thanks

Answer Source

In urlCreator you used if statement to check, if action is edit, and if it is, you add param id to your button url. Otherwise it doesnt have one, so there's two solutions:

Remove if statement from:

'urlCreator' => function ($action, $model, $key, $index) {
     $url = Url::toRoute(['vatout-faktur-out/add-data', 'id' => $model->fakturId]);
     return $url;
 }

Or remove$id from your actionAddData() - because youre not using it at all:

public function actionAddData() {
    $model = new VatoutFakturOut();
    return $this->render('addData', [
                'model' => $model,
    ]);
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download