0

Here is yii2 drop down list.

 <?php  echo $form->field($model, 'param1')->dropDownList(
            ArrayHelper::map(Model::find()->all(),'param1','param2');

It makes drop Down list with values of param1s and text to choose from of param2s. So you see param2 texts , choose one and corresponding param1 value goes to server.

No my problem is that I want to do the same but show user not only param2 text but I want text to be constructed from param2+param3.

example of what I want.

hidden value___________text

1_____________________alpha

2_____________________bravo

3_____________________lima

hidden value___________text

1_____________________alpha-red

2_____________________bravo-white

3_____________________lima-blue

Is that possible ?

David
  • 4,332
  • 13
  • 54
  • 93
  • Possible duplicate of [drop-down list for multiple values concat in one line](http://stackoverflow.com/a/27769661/428543) – topher May 12 '15 at 11:41

1 Answers1

1

One of the ways to do it will be using built-in ArrayHelper with toArray() method.

Put this in your model:

use yii\helpers\ArrayHelper;

...

public static function getList()
{
    $initialModels = static::find()->all();
    $models = ArrayHelper::toArray($initialModels, [
        'app\models\YourModel' => [
            'param1',
            'combinedParam' => function ($model) {
                return "$model->param2 - $model->param3";
            },
        ],
    ]);

    return ArrayHelper::map($models, 'param1', 'combinedParam');
}

Displaying it in view:

<?= $form->field($model, 'param1')->dropDownList(YourModel::getList()) ?>
David
  • 4,332
  • 13
  • 54
  • 93
arogachev
  • 33,150
  • 7
  • 114
  • 117