title: How to implement single search form in yii2
body: <p>Yii2 has a <code>searchModel</code> to search each field in the <code>GridView</code>. Is it possible to just create a single search field outside the <code>GridView</code> where the user can input keywords and by the time Search button is hit, the results will display in the <code>GridView</code> based on the keywords entered.</p>
  
  <p>CONTROLLER</p>
  
  <pre><code>public function actionIndex()
  {
      $session = Yii::$app-&gt;session;
      //$searchModel = new PayslipTemplateSearch();
  
      $PayslipEmailConfig = PayslipEmailConfig::find()-&gt;where(['company_id'=&gt; new \MongoId($session['company_id'])])-&gt;one();
  
      $payslipTemplateA = PayslipTemplate::find()-&gt;where(['company_id' =&gt; new \MongoId($session['company_id'])])-&gt;andwhere(['template_name' =&gt; 'A'])-&gt;one();
      $payslipTemplateB = PayslipTemplate::find()-&gt;where(['company_id' =&gt; new \MongoId($session['company_id'])])-&gt;andwhere(['template_name' =&gt; 'B'])-&gt;one();
  
      $pTemplateModel = PayslipTemplate::find()-&gt;where(['company_id' =&gt; new \MongoId($session['company_id'])])-&gt;all();
      $user = User::find()-&gt;where(['_id' =&gt; new \MongoId($session['user_id'])])-&gt;one();
      $module_access = explode(',', $user-&gt;module_access);
  
      //$dataProvider = User::find()-&gt;where(['user_type' =&gt; 'BizStaff'])-&gt;andwhere(['parent' =&gt; new \MongoId($session['company_owner'])])-&gt;all();
      $searchModel = new UserSearch();
      $dataProvider = $searchModel-&gt;search(Yii::$app-&gt;request-&gt;queryParams);
  
      return $this-&gt;render('index', [
          'PayslipEmailConfig' =&gt; $PayslipEmailConfig,
          'dataProvider' =&gt; $dataProvider,
          'payslipTemplateA' =&gt; $payslipTemplateA,
          'payslipTemplateB' =&gt; $payslipTemplateB,
          'searchModel' =&gt; $searchModel,
      ]);
  }
  public function actionSearchresults($keyword)
  {
      $session = Yii::$app-&gt;session;
      if ( $keyword == '') {
          return $this-&gt;redirect(\Yii::$app-&gt;request-&gt;getReferrer());
      } else {
          $user = User::find()-&gt;where( [ '_id' =&gt; new \MongoId($id) ] )-&gt;one(); 
          $searchModel = new PayslipTemplateSearch();
  
          $payslipTemplateA = PayslipTemplate::find()-&gt;where(['company_id' =&gt; new \MongoId($session['company_id'])])-&gt;andwhere(['template_name' =&gt; 'A'])-&gt;one();
          $payslipTemplateB = PayslipTemplate::find()-&gt;where(['company_id' =&gt; new \MongoId($session['company_id'])])-&gt;andwhere(['template_name' =&gt; 'B'])-&gt;one();
  
          return $this-&gt;render('searchresults', [
              'searchModel' =&gt; $searchModel,
              'user' =&gt; $user,
              'payslipTemplateA' =&gt; $payslipTemplateA,
              'payslipTemplateB' =&gt; $payslipTemplateB,
          ]);
      }    
  }
  </code></pre>
  
  <p>I asked a question connected to this problem here: <a href="https://stackoverflow.com/questions/32882097/main-search-form-in-yii2">Main Search Form in Yii2</a></p>
  
  <p>It didn't due to some complications in Kartik's <code>Select2</code> search dropdown widget. Now I switched temporarily to a simple Yii2 search field.</p>
  
  <p>VIEW</p>
  
  <pre><code>echo $form-&gt;field($model, '_id')-&gt;textInput(array('placeholder' =&gt; 'search'))-&gt;label(false);
  </code></pre>
  
  <p>MODEL</p>
  
  <pre><code>&lt;?php
  
  namespace app\models;
  
  use Yii;
  use yii\base\Model;
  use yii\data\ActiveDataProvider;
  use app\models\User;
  
  /**
   * UserSearch represents the model behind the search form about `app\models\User`.
   */
  class UserSearch extends User
  {
      /**
       * @inheritdoc
       */
      public function rules()
      {
          return [
              [[/*'_id',*/ 'creator_id'], 'integer'],
              [['fname', 'lname', 'email', 'username', 'user_type'], 'safe'],
          ];
      }
  
      /**
       * @inheritdoc
       */
      public function scenarios()
      {
          // bypass scenarios() implementation in the parent class
          return Model::scenarios();
      }
  
      /**
       * Creates data provider instance with search query applied
       *
       * @param array $params
       *
       * @return ActiveDataProvider
       */
      public function search($params)
      {
          $session = Yii::$app-&gt;session;
  
          $query = User::find();
          $query-&gt;where(['user_type' =&gt; 'BizStaff'])-&gt;andwhere(['parent' =&gt; new \MongoId($session['company_owner'])]);
  
          $dataProvider = new ActiveDataProvider([
              'query' =&gt; $query,
          ]);
  
          $this-&gt;load($params);
  
          if (!$this-&gt;validate()) {
              // uncomment the following line if you do not want to any records when validation fails
              // $query-&gt;where('0=1');
              return $dataProvider;
          }
  
          $query-&gt;andFilterWhere([
              '_id' =&gt; $this-&gt;_id,
              'creator_id' =&gt; $this-&gt;creator_id,
          ]);
  
          $query-&gt;andFilterWhere(['like', 'fname', $this-&gt;fname])
              -&gt;andFilterWhere(['like', 'lname', $this-&gt;lname])
              -&gt;andFilterWhere(['like', 'email', $this-&gt;email])
              -&gt;andFilterWhere(['like', 'username', $this-&gt;username])
              -&gt;andFilterWhere(['like', 'user_type', $this-&gt;user_type]);
  
          return $dataProvider;
      }
  }
  </code></pre>
  
  <p>Do you have any idea on how to I implement a single search? It's kind of a smarter search since it can search everything in the database table based on keywords inputted.</p>
  
  <p>EDIT</p>
  
  <p>When I search a keyword, say for example 'hello', it then gives me this url and error after hitting enter key:</p>
  
  <p>URL: </p>
  
  <blockquote>
    <p><a href="http://localhost/iaoy-dev/web/index.php?r=payslip-template%2Fsearchresults&amp;PayslipTemplateSearch%5B_id%5D=hello" rel="nofollow noreferrer">http://localhost/iaoy-dev/web/index.php?r=payslip-template%2Fsearchresults&amp;PayslipTemplateSearch%5B_id%5D=hello</a></p>
  </blockquote>
  
  <p>Error message:</p>
  
  <blockquote>
    <p>Bad Request (#400) Missing required parameters: id</p>
  </blockquote>
  
  <p>Help.</p>