Yii 2 GridView Search Within ActiveForm

468 Views Asked by At

Working with Yii 2 ActiveForm, I am trying to add a GridView with search for selecting items to add to a relationship (the database is MongoDB)

I want to include a "Search and Select" GridView widget, and have the items added to the ActiveForm model as an array of ids.

For Example:

<?php $form = ActiveForm::begin();?>
    <?=$form->field($model, 'summary')->textInput()?>

<?=
GridView::widget([
    'id' => 'productSearch',
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'pjax' => true,
    'columns' => [
        'name',
    ],
])
?>
<!-- Display the Assigned Products selected by productSearch --> 
    <?=
GridView::widget([
    'id' => 'assignedProducts',
    'dataProvider' => $model->AssignedProducts,
    'columns' => ([
        'name',
    ]),
]);?>

<?php ActiveForm::end();?>

However when I include the GridView search within the ActiveForm::begin() / ActiveForm::end() tags, the "POST" that gets called triggers the form.

Are there any guides on doing this sort of setup?

Or do I need to create the entire form from scratch?

I know I will most likely have to use a bit of jQuery to pull the 'Selected' items from the GridView when they are selected correct?

1

There are 1 best solutions below

0
Senthil On

If you are using GridView with Search, you should use <?= Html::beginForm() ?> rather than ActiveForm. To get the checked boxes, instead of Javascript you can instead get the ids of checked boxes on form post. Check this out: How I can process a checkbox column from Yii2 gridview?