adding filter to gridview widget

I am trying to add filter to Gridview widget included in _form.php

The grid is showing fine, even filter field is shown,but the filter is not working.

Here is my code.




<?php  

   $searchModel = New CitySearch(); ?>

    

    

<?= GridView::widget([

        'dataProvider' => $dataProvider,

        'filterModel' => $searchModel,

        'columns' => [

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


          //  'id',

            'city_name',


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

        ],

    ]); ?>   




Ok I have found the solution, I just needed to add one line to get it to work.




<?php  

   $searchModel = New CitySearch();

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


?>

    

    

<?= GridView::widget([

        'dataProvider' => $dataProvider,

        'filterModel' => $searchModel,

        'columns' => [

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


          //  'id',

            'city_name',


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

        ],

    ]); ?>