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

Empty field with Ajax and multiple send string #304

Closed omsi668 closed 4 years ago

omsi668 commented 4 years ago

Prerequisites

Steps to reproduce the issue

Hello,

When I updated the plugin from 2.1.4 to 2.1.6 I have a bunch of fields not working anymore and can't find a proper solution to it (I then expect it's a bug). The context is a page with 3 Select2 plugins, all optional, all with multiple and Ajax. Everything was working fine with 2.1.4.

This is the code of one of them :

Select2::widget([ 'data' => $skills, 'options' => [ 'multiple' => true, 'class' => 'form-control', ], 'model' => $searchModel, 'attribute' => 'skills', 'pluginOptions' => [ 'minimumInputLength' => 2, 'ajax' => [ 'url' => Yii::$app->urlManager->createUrl(['/ajax/find-skill']), 'processResults' => new JsExpression(AjaxController::processResultsFindJs()), ], 'language' => [ 'errorLoading' => new JsExpression("function () { return 'Waiting for results...'; }"), ], ], ]);

$skills is populated with an array of preselected values if you come to the form already populated.

This form will populate a searchFilter where $skills is declared as an attribute of searchFilter as public $skills = [];

No specific rules are set to skills attribute except "safe"

Then we populate the skills attribute of the searchFilter with the load() method. (I wrote everything but in the end this is just classic search, no extra JS, no fancy method)

The problem is that, if I don't fill any skill in my search, select2 will still send an empty string, and then populate the field with an empty token (see attachment). empty_token

How to avoid that?

Thank you

Libraries

Isolating the problem

phrakon commented 4 years ago

The similar issue after update: multiple=>false + ajax: can't preset value by initValueText while pluginOptions['placeholder'] exists. that placeholder should be dropped at all or 'placeholder' => !$model->gallery_id ? 'Select item...' : null, Anyway something has been broken...

kartik-v commented 4 years ago

Check if it is resolved with the latest fix? Else will reopen.

phrakon commented 4 years ago

Seems it works.

omsi668 commented 4 years ago

Hello,

I tried the master branch and the bugs seems to be still there (i still see empty placeholders)

kartik-v commented 4 years ago

I pushed in a small update...

@omsi668 can you please share your configuration and code sample to reproduce your issue? Currently cannot reproduce your use case.

omsi668 commented 4 years ago

Hello,

With this latest fix it seems to work, i don't manage to make it bug again thank you.

Just for information if you want to build a test case i'll still answer your questions as much as possible :



- This is obviously with dev-master 58d9a32

Thank you