Using bootstrap multiselect in the form, it is necessary to substitute the values ​​from the address bar into the multi-select. To transfer a single value, I used the following construction:

<?php $value = \Yii::$app->request->get($name) ?: ''; ?> <div class="<?= $class ? $class : "col-sm-3 col-xs-12" ?>"> <label><?= $label ?></label> <select name="<?= $name ?>" multiple="multiple"> <?php foreach ($source as $id => $val) { ?> <option <?= $value == $id ? 'selected="selected"' : '' ?> value="<?= $id ?>"><?= $val ?></option> <?php } ?> </select> </div> 

Now the array is transferred, how to implement the substitution of all array values?

  • What does an array look like in the address bar? - Yaroslav Molchan
  • & multiselect_region [] = 417899 & multiselect_region [] = 407436 & multiselect_region [] = 357975 One of the examples is Aleksandr Ivankovich

2 answers 2

Try the following option:

 <?php $values = \Yii::$app->request->get($name) ?: []; ?> <div class="<?= $class ? $class : "col-sm-3 col-xs-12" ?>"> <label><?= $label ?></label> <select name="<?= $name ?>" multiple="multiple"> <?php foreach ($source as $id => $val) { ?> <option <?= in_array($id, $values) ? 'selected="selected"' : '' ?> value="<?= $id ?>"><?= $val ?></option> <?php } ?> </select> </div> 
  • Unfortunately, it did not work, nothing happened, print_r ($ values) did not output anything. - Aleksandr Ivankovich
  • $ name specified multiselect_region ? or with brackets at the end? you only need to specify the name - Yaroslav Molchan
  • Yes, the bad one did not think that the $ name is substituted with just brackets, thanks. - Aleksandr Ivankovich

You can also use yii\helpers\Html :

Html::dropDownList($name', $values, $source, ['multiple' => true])