B
B
begreeze2019-07-01 12:00:27
Yii
begreeze, 2019-07-01 12:00:27

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();

The action in the form is specified as ['index']. If I conduct a search, then the controller naturally swears that the required parameters are missing: course_id. And the search for a specific student needs to be carried out for a specific course
. Here is the controller:
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
        ]);
    }

The search form is universal, i.e. rendered on different pages. Somewhere get-parameters can be, somewhere not. If they are, then they are not the same and their number is different.
Is it possible to set an action in the search form along with GET parameters?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaly Khomenko, 2019-07-01
@begreeze

Is it possible to set an action in the search form along with GET parameters?

Quite Or you can add a hidden course ID field. Or something else. There are many options, depending on the context of the task and conditions
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
        ]);
    }

D
Dmitry, 2019-07-01
@slo_nik

If I conduct a search, then the controller naturally swears that the required parameters are missing: course_id.

Are you using andWhere() in your search model?
For those data that may not be in the request, use andFilterWhere (), then it will not swear at the lack of data.
Why pass a parameter to the action, which may not be in the request?
Remove $course_id from required parameter and get data from string.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question