0

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?

jordan.baucke
  • 4,308
  • 10
  • 54
  • 77
  • i am trying to understand that if you are using the first gridview to search and on selecting any item you want it to appear in the next `GridView` below it? correct? – Muhammad Omer Aslam Jul 22 '18 at 11:40
  • I dont think you need to mix up two widgets ActiveForm and GridView. You only need Gridview and define relations inside the SearchModel. There are lots of examples on the internet, e.g. https://www.yiiframework.com/doc/guide/2.0/en/output-data-widgets. ActiveForm is used for inserting new or updating records, not for searching. – lubosdz Jul 22 '18 at 12:00
  • @lubosdz that is not correct thing to say that ActiveForm cant be used for Search you generate the default CRUD from GII and get the _serch.php file for searching or dont ? – Muhammad Omer Aslam Jul 22 '18 at 12:58

1 Answers1

0

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?

Senthil
  • 426
  • 5
  • 11