{"record":{"id":"166f4f0de6a821cd","repo":"theonedev/onedev","slug":"val-cannot-be-called-if-initselection-is-not-d","errorCode":null,"errorMessage":"val() cannot be called if initSelection() is not defined","messagePattern":"val\\(\\) cannot be called if initSelection\\(\\) is not defined","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"server-core/src/main/java/io/onedev/server/web/component/select2/res/select2.js","lineNumber":2670,"sourceCode":"               this.updateSelection([]);\n               this.clearSearch();\n               if (triggerChange) {\n                   this.triggerChange();\n               }\n               return;\n           }\n\n           // val is a list of ids\n           this.setVal(val);\n\n           if (this.select) {\n               this.opts.initSelection(this.select, this.bind(this.updateSelection));\n               if (triggerChange) {\n                   this.triggerChange();\n               }\n           } else {\n               if (this.opts.initSelection === undefined) {\n                   throw new Error(\"val() cannot be called if initSelection() is not defined\");\n               }\n\n               this.opts.initSelection(this.opts.element, function(data){\n                   var ids=$(data).map(self.id);\n                   self.setVal(ids);\n                   self.updateSelection(data);\n                   self.clearSearch();\n                   if (triggerChange) {\n                       self.triggerChange();\n                   }\n               });\n           }\n           this.clearSearch();\n       },\n\n       // multi\n       onSortStart: function() {\n           if (this.select) {","sourceCodeStart":2652,"sourceCodeEnd":2688,"githubUrl":"https://github.com/theonedev/onedev/blob/d44925c47c37992c828ea673a5f9620539bc3ff2/server-core/src/main/java/io/onedev/server/web/component/select2/res/select2.js#L2652-L2688","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Provide an initSelection callback in the select2 options that resolves the given element/value into option data.","If data is local, use a data array option instead of query so initSelection is not needed.","Set the initial value via a <option selected> for selects, or defer val() until selection data can be resolved.","Upgrade to a newer select2 (or the version bundled with the framework) where AJAX sources resolve initial values differently."],"exampleFix":"// before\n$('#sel').select2({ query: queryFn });\n$('#sel').select2('val', '42');\n// after\n$('#sel').select2({\n  query: queryFn,\n  initSelection: function(element, callback) {\n    queryFn({ id: element.val(), callback: callback });\n  }\n});\n$('#sel').select2('val', '42');","handlingStrategy":"validation","validationCode":"if (!$el.is('select') && !$el.data('select2').opts.initSelection) {\n  throw new Error('initSelection required before calling val()');\n}","typeGuard":"function supportsVal(el) { const s2 = el.data('select2'); return !!s2 && !!s2.opts.initSelection; }","tryCatchPattern":"try { $el.select2('val', ids); } catch (e) { if (/initSelection/.test(e.message)) { /* initialize with data array or initSelection */ } else { throw e; } }","preventionTips":["Always define initSelection when using query-based select2 sources","Prefer a data array over query for local datasets","Test programmatic val() on widget initialization"],"tags":["javascript","select2","frontend"],"backgroundTag":"missing-required-config-field","analyzedSha":"d44925c47c37992c828ea673a5f9620539bc3ff2","analyzedAt":"2026-09-06T07:18:27.995Z","contentChangedAt":"2026-09-06T07:18:27.995Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}