Answer the question
In order to leave comments, you need to log in
How to set action in search form with additional GET parameters Yii2 Framework?
Hello.
There is a list of courses. And there is a list of students for a particular course.
On the student list page (url: mydomain.com/course-students/index?course_id=1)
I have a search form:
$form = ActiveForm::begin(['id'=>'search-form', 'action'=>['index'], 'method'=>'get', 'fieldConfig'=>['options'=>['tag'=>false]]]);
echo Html::beginTag('div', ['class'=>'input-group', 'style'=>'width: 300px;']);
echo $form->field($model, 'search')->textInput(['class'=>'form-control pull-right', 'placeholder'=>'Поиск...'])->label(false);
echo Html::beginTag('span', ['class'=>'input-group-btn']);
echo (isset($model->search)) ? Html::a('<i class="fa fa-remove"></i>', $form->action, ['class'=>'btn btn-default reset']) : '';
echo Html::submitButton('<i class="fa fa-search"></i>', ['class'=>'btn btn-default']);
echo Html::endTag('span');
echo Html::endTag('div');
$form->end();
public function actionIndex($course_id)
{
$searchModel = new CourseStudentsSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$dataProvider->query->andWhere(['course_id'=>$course_id]);
return $this->render('index', [
'searchModel'=>$searchModel,
'dataProvider'=>$dataProvider
]);
}
Answer the question
In order to leave comments, you need to log in
Is it possible to set an action in the search form along with GET parameters?
public function actionIndex($course_id = null)
{
$searchModel = new CourseStudentsSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
if (null === $course_id) {
// Что делаем?
}
$dataProvider->query->andWhere(['course_id'=>$course_id]);
return $this->render('index', [
'searchModel'=>$searchModel,
'dataProvider'=>$dataProvider
]);
}
If I conduct a search, then the controller naturally swears that the required parameters are missing: course_id.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question