2

I have a form that uses selected rows in a Gridview as form input. The problem is that when I try searching the Gridview, it submits the form. I have tried enclosing the Gridview in Pjax and checking whether the request isPjax in the controller. It does not seem to be working, maybe I am missing something.

What I would like to be able to do is have users search the form for the relevant data, select it, select an item from the dropdownlist and then submit the form. Any suggestions?

This is my view:

    <?php $form = ActiveForm::begin([]) ?>
    <?php \yii\widgets\Pjax::begin() ?>
    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
            ['class' => 'yii\grid\CheckboxColumn'], //to select rows for the form
            'name',
            [
                'attribute' => 'manager.name',
                'label' => 'Manager',
            ],
        ],
    ]); ?>
    <?php \yii\widgets\Pjax::end() ?>
    <?= $form->field($model, 'id_questionnaire')->dropDownList($questionnaireItems) ?>
        <div class="form-group">
            <?= Html::submitButton(Yii::t('app', 'Submit'), ['class' => 'btn btn-success btn-block']) ?>
        </div>
    <?php ActiveForm::end() ?>

In my controller, I check for Pjax first but this does not work:

    if(Yii::$app->request->isPjax){
        //search
    }
    if($model->load(Yii::$app->request->post())) {
        if (Yii::$app->request->post('selection')) {
            foreach (Yii::$app->request->post('selection') as $id) {
                //save the model
            }
        }
        return $this->redirect(['questionnaire/index']);
    }
NullError
  • 71
  • 2
  • 11
  • have look at this - http://stackoverflow.com/questions/28992972/yii2-how-to-properly-create-checkbox-column-in-gridview-for-bulk-actions – JKLM May 19 '16 at 18:43
  • @Saurabh Thanks! Using the Html form instead of Activeform works. I wish there was a way to still use Activeform though, but this will work for now. – NullError May 20 '16 at 11:12
  • great to hear that :) – JKLM May 20 '16 at 11:14

0 Answers0