View multiple selection data on yii2

Hello guys, I need help with my visualization!
I used Select2 to create my form, but in my view, country codes are appearing instead of country data.
I’ve tried in many ways to show but I couldn’t, someone could help me

 <?php
        echo $form->field($model, 'impacted_countries')->widget(Select2::classname(), [
            'data' => [
                "001" => "Mundo",
                "002" => "África",
                "003" => "América do Norte",
                "005" => "América do Sul",
                "009" => "Oceania",
                "011" => "África Ocidental",
                "013" => "América Central",
                "014" => "África Oriental",
                "015" => "África do Norte",
                "017" => "África Central",
                "018" => "África Meridional",
                "019" => "Américas",
            ],
            'options' => [
                'placeholder' => 'Selecione os países',
                'multiple' => true
            ],
            'pluginOptions' => [
                'allowClear' => true,
                'tags' => true,
                'tokenSeparators' => [',', ' '],
            ],
        ])
        ?>    

in my view is

            [
            'attribute' => 'impacted_countries',
            'visible' => !empty($model->impacted_countries)
        ],

I am viewing the codes but I need to see the name of the countries, someone who could help me please

Look that in your view you are displaying the content that is in $model->impacted_countries, which probably is just the number.

You should do a conversion of that number to the correspondent value in that data array, which can be done in lots of different ways.

Personally I like to create a method inside the model class that do this conversion, which can improve readability:

public function getImpactedCountriesLabel() {
    $data = [
        "001" => "Mundo",
        // ...
        "019" => "Américas",
    ];
    return $data[$model->impacted_countries] ?? '';
}

Add the impactedCountriesLabel to your model rules.

Then in your view you just need to change the attribute called

[
    'attribute' => 'impactedCountriesLabel',
    'visible' => !empty($model->impacted_countries),
],
1 Like

Hi @leofilipesilva,

I would do it like the following. It’s basically the same idea with @bpanatta.

First, I would write the definition of the countries and some methods in the model:

class Something extends ActiveRecord
{
    ...

    public static $countries = [
        "001" => "Mundo",
        "002" => "África",
        "003" => "América do Norte",
        ...
        "019" => "Américas",
    ];

    public static function getCountryName($code)
    {
        if (array_key_exists($code, self::$countries)) {
            return self::$countries[$code];
        } else {
            return $code;
        }
    }

    public function getImpactedCountryNames($glue)
    {
        $codes = preg_split('/[\s,]+/', $this->impacted_countries);
        $names = [];
        foreach($codes as $code) {
            $names[] = self::getCountryName($code);
        }
        return implode($glue, $names);
    }
}

The class name must not be Something, it’s up to you.

Then I would use them in the form:

        echo $form->field($model, 'impacted_countries')->widget(Select2::classname(), [
            'data' => Something::$countries,
            'options' => [
                'placeholder' => 'Selecione os países',
                'multiple' => true
            ],
            ...

and in the view:

        [
            'attribute' => 'impacted_countries',
            'value' => $model->getImpactedCountryNames("\n"),
        ],
2 Likes

@bpanatta thank you very much for your contribution and response! Thanks to your answer I arrived at the solution!

1 Like

@softark thank you very much for your contribution and response! Your answer, in addition to being complete, also clarified me and helped improve the code!