2amigos / yii2-selectize-widget

Selectize From Brian Reavis Yii2 Widget
Other
73 stars 41 forks source link

Usage with ActiveForm field #4

Closed RomeroMsk closed 10 years ago

RomeroMsk commented 10 years ago

Hello! Can you show me how to use your widget with ActiveForm field? I have this code now:

$form = ActiveForm::begin(...);
echo $form->field($searchModel, 'name')->listBox([...], ['multiple' => true]);

Can't I write something like this?

echo $form->field($searchModel, 'name')->selectize([...]);
tonydspaniard commented 10 years ago

No you can't... you have to use the following format:

echo $form->field($searchModel, 'name')->widget(Selectize::className(), [...]);
RomeroMsk commented 10 years ago

That's what I need, thank you.

RomeroMsk commented 10 years ago

How can I make multiselect when using selectize via widget() method? The only way I've found:

            <?= $form->field($searchModel, 'status_id')->widget(Selectize::className(), [
                'items' => ArrayHelper::getColumn(Structure::instance()->statuses, 'name'),
                'clientOptions' => [
                    'maxItems' => null, // This will convert single select to multiple
                ],
            ]) ?>

But in this case your widget will generate single 'select' tag with first option selected and only after that selectize.js will convert itself to multiselect, so first option will be selected: image

RomeroMsk commented 10 years ago

If I set 'multiple' attribute on input via 'inputOptions', widget will reset it. I think, the best way to use multiselects with your widget is adding a config option and checking it in run() method. Something like this:

public function run()
{
    if ($this->multiple) {
        $this->options['multiple'] = true;
    }
    ...
}