Skip to content

Empty field with Ajax and multiple send string #304

Closed
@omsi668

Description

@omsi668

Prerequisites

  • I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
  • The issue still exists against the latest master branch of yii2-widget-select2.
  • This is not an usage question. I confirm having read the widget documentation and demos.
  • This is not a general programming / coding question. (Those should be directed to the webtips Q & A forum).
  • This is not a source plugin (select2) issue. (Those should be directed to the plugin issues repo).
  • I have attempted to find the simplest possible steps to reproduce the issue.
  • I have included a failing test as a pull request (Optional).

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

  • jQuery version: 3.4.1
  • yii2-widget-select2 version: 2.1.6

Isolating the problem

  • This bug happens on the widget demos page
  • The bug happens consistently across all tested browsers
  • This bug happens when using yii2-widget-select2 without other plugins
  • I can reproduce this bug in a jsbin

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions