Answer the question
In order to leave comments, you need to log in
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' => 'Расход']) ?>
$searchModel = new ModelSearch();
$dataProvider = $searchModel->search(Yii::$app->request->post());
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;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question