kartik-v / yii2-widget-select2

Enhanced Yii2 wrapper for the Select2 jQuery plugin (sub repo split from yii2-widgets).
http://demos.krajee.com/widget-details/select2
Other
323 stars 145 forks source link

issue with tags #184

Closed albertborsos closed 7 years ago

albertborsos commented 7 years ago

Prerequisites

Steps to reproduce the issue

  1. use with a model with any attribute
  2. paste the following snippet to your form
    <?= $form->field($model, 'quick_replies')->widget(\kartik\select2\Select2::className(), [
    'data' => $model->getQuickRepliesToSelect2(), // empty array
    'options' => [
        'multiple' => true,
    ],
    'pluginOptions' => [
        'tags' => true,
        'minimumInputLength' => 1,
        'tokenSeparators' => [',', ' '],
    ],
    ]) ?>
  3. reload the form

Expected behavior and actual behavior

When I follow those steps, I see that after the second item the first 2 disappear: select2 1

I was expecting it does not disappear.

Environment

Browsers

Operating System

Libraries

Isolating the problem

albertborsos commented 7 years ago

This one is OK, but i need to write the surrounding template, and the validation does not work.

<?= \kartik\widgets\Select2::widget([
    'id' => Html::getInputId($model, '[' . $id . ']quick_replies'),
    'name' => Html::getInputName($model, '[' . $id . ']quick_replies'),
    'options' => [
        'multiple' => true,
    ],
    'value' => $model->getQuickRepliesToSelect2(),
    'data' => [],
    'pluginOptions' => [
        'tags' => true,
        'allowClear' => true,
        'minimumInputLength' => 1,
        'tokenSeparators' => [',', ' '],
    ],
]) ?>
kartik-v commented 7 years ago

Not completely clear with how you are rendering the HTML markup. It seems you may have duplicate identifiers (HTML id attribute) for select2 widget input on the same page which would cause the plugin to break and you need to keep it unique.

For example tagging similar to your code works on the demo page here.

albertborsos commented 7 years ago

No, there is only one id. On the demo page there is an example with static widget. But i have issue when i use with ActiveForm instance like $form->field($model, 'attribute')->widget(Select2::className(), $config)

I will make a live example later.

kartik-v commented 7 years ago

The first example on the demo page for tags usage is a widget with model and active form.

albertborsos commented 7 years ago

OK, I found it. There was a trim filter in the model's rule method. After I removed it, then the widget starts working as expected.