Shaeldon
Shaeldon

Reputation: 883

Dynamic Checkbox List in Yii2

So I'm trying to create a dynamic Checkbox List in Yii2. I'm trying to adept the work done here but without success. As described there i should use the ArrayHelper.

So first I am fetching the data (works)

$listData=ArrayHelper::map(EinsatzorteModel::find()->asArray()->all(),'id','location'); 

Output(shortend):

array(9) { 
   [1]=> string(18) "Region: Y" 
   [2]=> string(17) "Region: X"
}

Now I'm trying to add it to a checkbox list:

<?= $form   ->field($model, 'locations[]')
           ->checkboxList(
            [$listData]
           )->label('Regionen');
?>

Which errors to:

htmlspecialchars() expects parameter 1 to be string, array given

Obviously its an Array, but shouldn't it fit anyway? The checkbosList expects parameters to be given as

'A' => 'Item A'

which actually should fit the format from the ArrayHelper.

So where do I get it wrong?

Upvotes: 0

Views: 1228

Answers (1)

soju
soju

Reputation: 25322

You should simply try $listData instead of [$listData].

Upvotes: 1

Related Questions