Alias
Alias

Reputation: 721

Get data from select2 and pass it to controller in yii2

I've one select2 form field, two datepickers and a search button in productnames index file. It is only to search data I'm unable to get the data selected in the select2 widget and pass it to controller which in turn can search other models.

index.php

<?php

use yii\helpers\Html;
use yii\grid\GridView;
use kartik\select2\Select2;
use dosamigos\datepicker\DatePicker;
use yii\helpers\ArrayHelper;
use frontend\modules\sbbtdtproduct\models\Productnames;
use yii\helpers\Json;

/* @var $this yii\web\View */
/* @var $searchModel frontend\modules\sbbtdtproduct\models\ProductnamesSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = 'Productnames';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="productnames-index">

    <h1><?= Html::encode($this->title) ?></h1>
    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>

    <div class="row">
        <div class="form-group">
            <div class="col-xs-5 col-sm-5 col-lg-5" >
                <?php
                    echo Select2::widget([
                    'model' => $model,
                    'attribute' => 'productnames_productname',
                    'data' => ArrayHelper::map(Productnames::find()->all(),'productnames_productname','productnames_productname'),
                    'options' => ['placeholder' => 'Select Product'],
                    'pluginOptions' => [
                        'allowClear' => true
                    ],
                    //'productname' => $productname,
                    ]);
                ?>
            </div>
            <div class="col-xs-3 col-sm-3 col-lg-3">
                <?= DatePicker::widget([
                    'name' => 'Start Date',
                    'attribute' => 'from_date',
                    'value' => '2014-01-31',
                    'template' => '{addon}{input}',
                        'clientOptions' => [
                            'autoclose' => true,
                            'format' => 'yyyy-mm-dd'
                        ]
                ]);?>
            </div>
            <div class="col-xs-3 col-sm-3 col-lg-3">
                <?= DatePicker::widget([
                    'name' => 'End Date',
                    'attribute' => 'to_date',
                    'value' => '2014-01-31',
                    'template' => '{addon}{input}',
                        'clientOptions' => [
                            'autoclose' => true,
                            'format' => 'yyyy-mm-dd'
                        ]
                ]);?>
            </div>
            <div class="col-xs-1 col-sm-1 col-lg-1" >

                <?= Html::a('Search', ['/sbbtdtproduct/production/index','productname' => $model['productnames_productname']], ['class'=>'btn btn-primary']) ?>
            </div>           
        </div>
    </div>
</div>

production controller action

public function actionIndex($productname)
    {
        $productname = yii::$app->request->get('productnames_productname');
        $searchModel = new ProductionSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams, $productname);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

productionsearch model

public function search($params,$productname)
    {
        $query = Production::find()
                ->where(['productname' => $productname]);

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'productionid' => $this->productionid,
            'productiondate' => $this->productiondate,
            'itemid' => $this->itemid,
            'prodqty' => $this->prodqty,
        ]);

        $query->andFilterWhere(['like', 'productname', $this->productname])
            ->andFilterWhere(['like', 'batchno', $this->batchno]);

        return $dataProvider;
    }

error - enter image description here update - enter image description here Database Log enter image description here The data base Log shows that no value has been passed from search model.

I can see the value selected in select2 or datepicker as below, but it's not passing to the controller.

enter image description here

enter image description here

Upvotes: 0

Views: 1958

Answers (1)

Nitin Pund
Nitin Pund

Reputation: 1092

1.on your view page You have not added form tag, so other parameters will not get posted, you must add everything inside form tag and submit that form, as follows

<?php

use yii\helpers\Html;
use yii\grid\GridView;
use kartik\select2\Select2;
use dosamigos\datepicker\DatePicker;
use yii\helpers\ArrayHelper;
use frontend\modules\sbbtdtproduct\models\Productnames;
use yii\helpers\Json;
use yii\widgets\ActiveForm;

/* @var $this yii\web\View */
/* @var $searchModel frontend\modules\sbbtdtproduct\models\ProductnamesSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = 'Productnames';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="productnames-index">

    <h1><?= Html::encode($this->title) ?></h1>
    <?php // echo $this->render('_search', ['model' => $searchModel]); 
    $form = ActiveForm::begin([
                'action' => ['/sbbtdtproduct/production/index'],
                'method' => 'post',
                'options' => ['data-pjax' => true],
                'enableClientValidation' => FALSE
    ]);
    ?>
    <div class="row">
        <div class="form-group">
            <div class="col-xs-5 col-sm-5 col-lg-5" >
                <?php
                    echo Select2::widget([
                    'model' => $model,
                    'attribute' => 'productnames_productname',
                    'data' => ArrayHelper::map(Productnames::find()->all(),'productnames_productname','productnames_productname'),
                    'options' => ['placeholder' => 'Select Product'],
                    'pluginOptions' => [
                        'allowClear' => true
                    ],
                    //'productname' => $productname,
                    ]);
                ?>
            </div>
            <div class="col-xs-3 col-sm-3 col-lg-3">
                <?= DatePicker::widget([
                    'name' => 'Start Date',
                    'attribute' => 'from_date',
                    'value' => '2014-01-31',
                    'template' => '{addon}{input}',
                        'clientOptions' => [
                            'autoclose' => true,
                            'format' => 'yyyy-mm-dd'
                        ]
                ]);?>
            </div>
            <div class="col-xs-3 col-sm-3 col-lg-3">
                <?= DatePicker::widget([
                    'name' => 'End Date',
                    'attribute' => 'to_date',
                    'value' => '2014-01-31',
                    'template' => '{addon}{input}',
                        'clientOptions' => [
                            'autoclose' => true,
                            'format' => 'yyyy-mm-dd'
                        ]
                ]);?>
            </div>
            <div class="col-xs-1 col-sm-1 col-lg-1" >
                <?= Html::submitButton('Search', ['class'=>'btn btn-primary']) ?>
                 <?php ActiveForm::end(); ?>
            </div>           
        </div>
    </div>
</div>

2.Controller

now inside your controller you can access parametes as follows

public function actionIndex()
    {
        $productname = Yii::$app->request->post('productnames_productname');
        $searchModel = new ProductionSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams, $productname);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

Upvotes: 1

Related Questions