Jan Beeck
Jan Beeck

Reputation: 315

How to grab the value of a dropDownList in Yii2?

I am not grabbing the selected value ($model->attribute) on a dropDownList in Yii2, what could be wrong? Thanks

This is the code that is located on a View:

    $command = $connection->createCommand('SELECT att1 
                                             FROM table
                                                ORDER By table_id ASC');
    $rows = $command->queryAll();
    $command->execute();

    $listData = ArrayHelper::index($rows, null, 'table_id');

Then on the same View I call to $listData

    <div class="row">
        <div class="col-lg-9"> 

            <?php Pjax::begin(['enablePushState' => false]); ?>

            <?php $form = ActiveForm::begin(['id' => 'test-form', 'options' => ['data-pjax' => true]
            ]); ?>

                <?= $form->field($model, 'attribute')->dropDownList($listData, ['prompt'=>'List of attributes.']); ?>


                <div class="form-group">
                    <?= Html::submitButton('Submit', ['class' => 'btn btn-primary', 'name' => 'test-button']) ?>
                </div>

            <?php ActiveForm::end(); ?>
            <?php Pjax::end(); ?>

        </div>
 </div>

This is the Controller:

public function actionTest()
    {
        $model = new TestForm();

            if ($model->load(Yii::$app->request->post()) && $model->validate()) {
                    $model->insertTest();
                 return $this->renderAjax('test', ['model' => $model]);       

            } else {
                return $this->renderAjax('test', ['model' => $model]);
            }

    } 

The Model has the definition of $attribute and insertTest() is a function that use the value of $attribute to query onto a DB.

Upvotes: 0

Views: 904

Answers (4)

Radhe9254
Radhe9254

Reputation: 198

you should use map in ArrayHelper

$listdata = ArrayHelper::map(CategoryModel::find()->orderBy(['table_id' => SORT_ASC])->all(), 'table_id', 'table_text');

Note: 'table_text' is table attribute that will appear in dropdown label.

Upvotes: 1

Jan Beeck
Jan Beeck

Reputation: 315

I found the solution to that issue.

By doing this way the form was not able to save the submitted information.

   $command = $connection->createCommand('SELECT att1 
                                                 FROM table
                                                    ORDER By table_id ASC');
   $rows = $command->queryAll();
   $command->execute();

  $listData = ArrayHelper::index($rows, null, 'table_id');

However, by this way the form is able to grab the value selected and put it into the variable.

$rows = table::find()->all();
$listData = ArrayHelper::map($rows,'table_id','att1');

Upvotes: 0

ScaisEdge
ScaisEdge

Reputation: 133370

You are selecting only att1 from table so you should map this column

$listData = ArrayHelper::index($rows, 'att1');

for debug try modify your actionTest commenting $model->insertTest(); and using $model->save(); if the value is saved in db then you should check inside you $model->insertTest() function

  public function actionTest()
  {
      $model = new TestForm();

          if ($model->load(Yii::$app->request->post()) && $model->validate()) {
                // $model->insertTest();
               $model->save();
               return $this->renderAjax('test', ['model' => $model]);       

          } else {
              return $this->renderAjax('test', ['model' => $model]);
          }

  } 

Upvotes: 1

HouseInTheForest
HouseInTheForest

Reputation: 96

Thinking of you should use

ArrayHelper::map($listData, 'table_id', 'table_id');

Because you need a one-dimensional array.

And it's better to use the ActiveRecord for queries to the database.

ArrayHelper Docs

Upvotes: 2

Related Questions