gridview form filters and common strings

Hello, I’m looking for a solution to what I’m finding to be a fairly hard-to-search-for problem.

In gridviews, when a property I define for an active model (that doesn’t come from a database column) contains a common string, the filter inputs reflect that string. For example, if I have a column “if1,” and all interfaces contain " - ", the filter will show " - " pre-entered. Is there a way to clear that filter when the page first loads, or otherwise make it so that the filter is not pre-populated?

I dumped the searchmodel and don’t see the string stored in it’s properties.

Controller Action:


   

    public function actionIndex()

    {

        $searchModel = new VirtualMachineSearch();

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

        $dataProvider = $searchModel->filter(Yii::$app->request->queryParams, $dataProvider->models);

        $dataProvider->sort = ['attributes' => ['vm_id', 'name', 'hostName', 'ipAddress', 'macAddress', 'json', 'if1', 'if$


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

            'searchModel' => $searchModel,

            'dataProvider' => $dataProvider,

        ]);

    }



Model getter/setter:




    public function getIf1()

    {

        return $this->macAddress .' - '.$this->ipAddress;

    }


    public function setIf1($value)

    {

        return $this->if1 = $value;

    }




Search model filter function:




    public function filter($params, $models)

    {

        foreach($models as $index => $model)

        {

            if(!isset($params['VirtualMachineSearch'])){break;}

            foreach($params['VirtualMachineSearch'] as $property => $string)

            {

                if( !empty($string) ){

                        if(!stristr($model->$property, $string) || empty($model->$property)){unset($models[$index]);}

                }

            }

        }


        $provider = new ArrayDataProvider([

                'allModels' => $models,

        ]);

        return $provider;

    }



On the search model, the only other change I made from the gii auto-generated values were to mark if1 safe.

UPDATE: if I comment out the section where I use my filter function, and only mark if1 safe, I still get the form prepopulated with the string

UPDATE: I think I understand better what was happening; The search is happening from the perspective of a new model, and as that new model gets initialized, the "if1" property was getting the string concatenated onto the other empty properties. To fix, I just changed the getter function to something like:




    public function getIf1()

    {

        $mac = $this->macAddress;

        $separator = empty($mac) ? null : ' - ';

        return $mac.$separator.$this->ipAddress;

    }



Hope this helps someone down the line