theonedev/onedev · error · Error

val() cannot be called if initSelection() is not defined

Error message

val() cannot be called if initSelection() is not defined

What it means

Select2's val() setter, when the element is not a <select> and the multi-select value is being set, requires an initSelection callback to translate values into option data. If opts.initSelection is undefined it throws this Error. Select2 cannot fetch the display data for the supplied ids without it.

Source

Thrown at server-core/src/main/java/io/onedev/server/web/component/select2/res/select2.js:2670

               this.updateSelection([]);
               this.clearSearch();
               if (triggerChange) {
                   this.triggerChange();
               }
               return;
           }

           // val is a list of ids
           this.setVal(val);

           if (this.select) {
               this.opts.initSelection(this.select, this.bind(this.updateSelection));
               if (triggerChange) {
                   this.triggerChange();
               }
           } else {
               if (this.opts.initSelection === undefined) {
                   throw new Error("val() cannot be called if initSelection() is not defined");
               }

               this.opts.initSelection(this.opts.element, function(data){
                   var ids=$(data).map(self.id);
                   self.setVal(ids);
                   self.updateSelection(data);
                   self.clearSearch();
                   if (triggerChange) {
                       self.triggerChange();
                   }
               });
           }
           this.clearSearch();
       },

       // multi
       onSortStart: function() {
           if (this.select) {

View on GitHub (pinned to d44925c47c)

Solutions

  1. Provide an initSelection callback in the select2 options that resolves the given element/value into option data.
  2. If data is local, use a data array option instead of query so initSelection is not needed.
  3. Set the initial value via a <option selected> for selects, or defer val() until selection data can be resolved.
  4. Upgrade to a newer select2 (or the version bundled with the framework) where AJAX sources resolve initial values differently.

Example fix

// before
$('#sel').select2({ query: queryFn });
$('#sel').select2('val', '42');
// after
$('#sel').select2({
  query: queryFn,
  initSelection: function(element, callback) {
    queryFn({ id: element.val(), callback: callback });
  }
});
$('#sel').select2('val', '42');
Defensive patterns

Strategy: validation

Validate before calling

if (!$el.is('select') && !$el.data('select2').opts.initSelection) {
  throw new Error('initSelection required before calling val()');
}

Type guard

function supportsVal(el) { const s2 = el.data('select2'); return !!s2 && !!s2.opts.initSelection; }

Try / catch

try { $el.select2('val', ids); } catch (e) { if (/initSelection/.test(e.message)) { /* initialize with data array or initSelection */ } else { throw e; } }

Prevention

When it happens

Trigger: Calling $(element).select2('val', value) on a non-select element (e.g. input[type=hidden]) initialized with a query-based data source but no initSelection option defined.

Common situations: Wicket select2 integrations setting an initial value programmatically on an AJAX-fed select2; copy-pasted select2 init code that defines query/data but omits initSelection; setting val() before the widget is configured.

Understand the failure class

Background: "is required", "must be set", "missing required field": configuration validation errors across open-source libraries — this error's family across 36 libraries.

Related errors


AI-assisted analysis of theonedev/onedev@d44925c47c (2026-09-06). Data as JSON: /api/errors/166f4f0de6a821cd. Report an issue: GitHub.