M
M
madphoenix2016-01-22 16:30:10
Yii
madphoenix, 2016-01-22 16:30:10

Pass values ​​from search form (checkboxList) to search model in Yii2?

Hello! Can you please tell me how to pass values ​​from the search form (checkboxList) to the search model in Yii2?
view:

<?= $form->field($searchModel, 'type')->checkboxList(['0' => 'Доход', '1' => 'Расход']) ?>

controller:
$searchModel = new ModelSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->post());

searchModel:
public function search($params)
    {
        $query = Payment::find()->orderBy('pay_date ASC');

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => false,
        ]);
  
        $this->load($params);
        if (!$this->validate()) {
            return $dataProvider;
        }

        if (is_array($this->type)) {
        
            $query->andFilterWhere(['in', 'type', array_keys($this->type)]);
        } 
        else {
            $query->andFilterWhere([
                'type' => $this->type
            ]);
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'category_id' => $this->category_id,
            'pay_date' => $this->pay_date,
            'status' => $this->status,
        ]);

        $query->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['user_id' => Yii::$app->user->identity->id]);

        return $dataProvider;
    }

But for some reason the array is not assigned. What could be the snag?
Thanks for the replies!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
madphoenix, 2016-01-22
@madphoenix

Got it! The problem was in the validation rules

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question