Dear All
Something is going wrong in the following use case.
I want a a select2, with multiple selections, with a default value selected when the page is loaded. In the select2 kartik documentation it is mentioned to use "initValueText" but something is going wrong here.
I get an exception:
array_combine(): Both parameters should have an equal number of elements
I have debugged and have come to the following line of code in the Select2.php file (starting from line code 184):
$this->initPlaceholder();
if (!isset($this->data)) {
if (!isset($this->value) && !isset($this->initValueText)) {
$this->data = [];
} else {
$key = isset($this->value) ? $this->value : ($multiple ? [] : '');
$val = isset($this->initValueText) ? $this->initValueText : $key;
$this->data = $multiple ? array_combine($key, $val) : [$key => $val];
}
}
It seems that the number of elements of $key and $val are not the same. The array for $val ( which equals $this->initValueText)- is set, but the $val (corresponding with $this->value) is not set.
I have tried adding
'value => [theValue]
in the array for the Select2 but it seems it gets skipped somehow.
The code I have (partially) for the select2 in the formgrid is as follows:
'attributes' => [
'klanten' => [ 'type' => Form::INPUT_WIDGET,
'widgetClass' => '\kartik\select2\Select2',
'model' => $model,
'attribute' => 'klanten',
'options' => [
'value'=> [$klant->klant_naam],
'initValueText' => [$klant->klant_naam],
'pluginOptions' => [
'multiple' => 'multiple',
'ajax' => [
'url' => 'http:///horizonmanager.test/klant/getklanten',
'dataType' => 'json',
'data' => new JsExpression('function(params) { return { q: params.term, page: params.page}; }'),
],
'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
'templateResult' => new JsExpression('function(klant) { return klant.text; }'),
'templateSelection' => new JsExpression('function (klant) { return klant.text; }'),
],
//'data' => $klanten,
'options' => [ 'placeholder' => 'Selecteer ...',
'multiple' => 'multiple'],
]
],
],