GridView with ActiveForm for filtering

Hello guys, I’m using Yii2 and I’m struggling to get a GridView working beside an ActiveForm.

I’m looking to use an ActiveForm not really to create any new model entry, but to filter GridView data. Yeah, I need now GridView already has filters, but I need them to be outside of the form, being hidden and only visible on a button click, that’s why I opted for an ActiveForm solution.

Any idea how should I implement this?


Use normal form:

echo Html::beginForm();


echo Html::endForm();

echo GridView::widget([



That would not work directly with GridView.

This is my current try:


use yii\grid\GridView;

use yii\widgets\Pjax;

use app\models\PagePost;

use yii\helpers\Url;

use yii\helpers\Html;

use yii\widgets\ActiveForm;

$baseUrl = Url::base(true);

$searchModel = new PagePost();




$form = ActiveForm::begin(['options' => ['data-pjax' => true ]]);

$_post = Yii::$app->request->post();

$searchModel->page_id = $page_id;

$searchModel->attributes = $_post;

if (!$searchModel->validate()) {

    echo '<pre>';




<?=$form->field($searchModel,'message'); ?>

<?= Html::submitButton('Submit', ['class'=> 'btn btn-primary']) ;?>

<div class="bg-white p-10 text-center">

<?php $dataProvider = $searchModel->search( $_post ); ?>

<?= GridView::widget([

    'dataProvider' => $dataProvider,

    'summary' => '<div class="text-left text-small m-b-10">A mostrar <strong>{begin}-{end}</strong> de <strong>{totalCount}</strong> resultados</div>',

    'headerRowOptions' => [

        'class' => 'text-small'


    'tableOptions' => [

        'class' => 'table table-condensed text-left'


    'rowOptions' => [

        'class' => 'v-a-middle'


    'footerRowOptions' => [

        'class' => 'text-center'


    'columns' => [






Pjax::end(); ?>


This does search correctly in the model, and filter the data in it, however I would like to have something to this without needing to press a button (like GridView filter) and I would like the value to be kept in the input when form is submit.

In the View:

<?php $form = ActiveForm::begin([

        'action' => ['index'],

        'method' => 'get',

    ]); ?>


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

<?= GridView::widget([



In the Controller:

$model = new Matchs();

$q = ...some empty/initial query...

if ( $model->load(Yii::$app->request->get()) ) {

   $q = //...Your query with form filters...


$dataProvider = new ArrayDataProvider([

            'key'=> 'id',

            'allModels' => $q,

            'sort' => ['attributes' => ['id',

            'matchdate', 'schedule','teams', 'goals','comments']],

            'pagination' => [

                'pageSize' => 15,



return $this->render('index', [

            'dataProvider' => $dataProvider, 'model' => $model]);