Z
Z
ZaurK2018-05-24 11:35:29
Yii
ZaurK, 2018-05-24 11:35:29

How to get value of selected array element in select in yii2?

Good afternoon! I don’t understand something, please tell me, I send a field with the selected select in the form:

<?php $items = [
                        '0' => 'Проект1',
                        '1' => 'Проект2',
                        '2' => 'Проект3',
                        ];
                       
                  ?>
                    <?= $form->field($model, 'service')->dropDownList($items) ?>

I send it by mail as a service field and get one of three values ​​​​(0, 2 or 3) depending on the selected one. How to get not array arguments, but their values ​​(Project1, Project2 or Project3)?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Timofeev, 2018-05-24
@ZaurK

And why do you put 0 , 1 and 2 in the values ​​if you want to get Project1, Project2 and Project3? Put what you need in there.

<?php 
$items = [
   'Проект1' => 'Проект1',
   'Проект2' => 'Проект2',
   'Проект3' => 'Проект3',
];

A
Anton Mashletov, 2018-05-24
@mashletov

Yes, at least make the model a static method

static function getServiceList() 
{
     return [ 'Проект1', 'Проект2', 'Проект3'];
}

And then
Well, actually then on this Model::getServiceList()[$id]get a name

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question