form and related gridview on the same page

Can any body help me how I can have a form and the related Gridview on the same page?


Form is a partial, you can call it into the page like so:

 <?= $this->render('_form', [

        'model' => $model,

        ]) ?>

For Gridview, you can bring in the related search form:

<?=$this->render('_search', ['model' => $searchModel])?>

Note the use of the echo tag <?=

Thank you so much Evercode. It is working just fine.

Only problem I am facing is the search fields are being rendered vertically and not horizontally.

Since I was trying the code you suggested on the index page which renders the gridview as such with filter, so there is repeat of the gridview.

Can your code can be modified in such a way that it renders just the Form without the gridview as I am using the page on the gridview page itself the repeat gridview will be omitted.


If you don’t want default search results to show on the index page, you could do it by inserting some if statements in the appropriate places in the searchmodel (search method), the controller and the view.

I looked in the guide and Gridview has a property showOnEmpty, which apparently you can set to false, but I did not see instructions on how to implement. So I came up with my own hacky approach, which works, but probably not best practice.

I’m going to list it here to see if anyone can provide a better implementation or if they know how to set showOnEmpty to false, which takes care of 50% of the coding.

Ok, so I had to change 3 things.

  1. Controller:

public function actionIndex()


        $searchModel = new UserSearch();

      if ($dataProvider = $searchModel->search(Yii::$app->request->queryParams)){

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

            'searchModel' => $searchModel,

            'dataProvider' => $dataProvider,



      } else {

          $dataProvider == false;

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

            'searchModel' => $searchModel,

            'dataProvider' => $dataProvider,






  1. searchModel:

public function search($params)


        $query = User::find();

        $dataProvider = new ActiveDataProvider([

            'query' => $query,



        if (($this->load($params) && $this->validate())) {



            'id' => $this->id,

            'created_at' => $this->created_at,

            'updated_at' => $this->updated_at,


        $query->andFilterWhere(['like', 'username', $this->username])

                    ->andFilterWhere(['like', 'email', $this->email])


        return $dataProvider;



  1. view


 if ($dataProvider){

   echo GridView::widget([

        'dataProvider' => $dataProvider,

        'filterModel' => $searchModel,

        'columns' => [

     	['class' => 'yii\grid\SerialColumn'],




       	// 'created_at',

            // 'updated_at',

            ['class' => 'yii\grid\ActionColumn'],


    ]); } ?>

If someone else has an easier answer to this, by all means, jump in, I would like to know too…