{"record":{"id":"0565a443992ecfe0","repo":"angular/components","slug":"listbox-has-selected-values-that-do-not-match-any","errorCode":null,"errorMessage":"Listbox has selected values that do not match any of its options.","messagePattern":"Listbox has selected values that do not match any of its options\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/cdk/listbox/listbox.ts","lineNumber":1025,"sourceCode":"          }\n          return;\n        }\n      }\n    });\n  }\n\n  /** Verifies that the option values are valid. */\n  private _verifyOptionValues() {\n    if (this.options && (typeof ngDevMode === 'undefined' || ngDevMode)) {\n      const selected = this.selectionModel.selected;\n      const invalidValues = this._getInvalidOptionValues(selected);\n\n      if (!this.multiple && selected.length > 1) {\n        throw Error('Listbox cannot have more than one selected value in single selection mode.');\n      }\n\n      if (invalidValues.length) {\n        throw Error('Listbox has selected values that do not match any of its options.');\n      }\n    }\n  }\n\n  /**\n   * Coerces a value into an array representing a listbox selection.\n   * @param value The value to coerce\n   * @return An array\n   */\n  private _coerceValue(value: readonly T[]) {\n    return value == null ? [] : coerceArray(value);\n  }\n\n  /**\n   * Get the sublist of values that do not represent valid option values in this listbox.\n   * @param values The list of values\n   * @return The sublist of values that are not valid option values\n   */","sourceCodeStart":1007,"sourceCodeEnd":1043,"githubUrl":"https://github.com/angular/components/blob/0411926e7d8ae06b32236ec1048a888cfad5abf2/src/cdk/listbox/listbox.ts#L1007-L1043","documentation":"CdkListbox._verifyOptionValues throws when the selected values contain entries that do not match any registered CdkOption values. Every selected value must correspond to an existing option's value, otherwise the listbox state is inconsistent.","triggerScenarios":"Setting listbox value (directly or via NgModel/FormControl) to values not present among the rendered cdkOption values; options loaded asynchronously after the value is set; values differing only by type (string '1' vs number 1) or case.","commonSituations":"Form values restored from persistence before the options render; list of options fetched from an API while the control keeps stale/old values; renaming an option's value without migrating saved selections.","solutions":["Ensure the value is set only after options are rendered (e.g. set value in/ngAfterContentInit or after the async options load).","Validate values against option values before assigning; filter out unknown values.","Fix type mismatches — compare the same type (string vs number) and casing as the cdkOption values.","If persisted selections may be stale, sanitize them against the current option list on load."],"exampleFix":"// before\nform.patchValue({ tags: ['stale-tag'] }); // no cdkOption with value 'stale-tag'\n// after\nconst valid = ['stale-tag'].filter(v => optionValues.includes(v));\nform.patchValue({ tags: valid });","handlingStrategy":"validation","validationCode":"const optionValues = new Set(options.map(o => o.value));\nconst validSelection = selected.filter(v => optionValues.has(v));\nif (validSelection.length !== selected.length) {\n  listbox.value = validSelection;\n}","typeGuard":"function allValuesMatch(selection: unknown[], options: {value: unknown}[]): boolean {\n  const known = new Set(options.map(o => o.value));\n  return selection.every(v => known.has(v));\n}","tryCatchPattern":"try {\n  listbox.value = savedSelection;\n} catch (e) {\n  if ((e as Error).message.includes('do not match any of its options')) {\n    listbox.value = savedSelection.filter(v => optionValues.has(v));\n  } else { throw e; }\n}","preventionTips":["Set listbox values only after options are rendered (ngAfterViewInit or after async load).","Sanitize persisted selections against the current option list on load.","Keep option value types consistent (avoid string vs number drift).","Write a dev-mode assertion in your service that stored values exist in the options source."],"tags":["angular","cdk","listbox","selection"],"backgroundTag":"invalid-option-value","analyzedSha":"0411926e7d8ae06b32236ec1048a888cfad5abf2","analyzedAt":"2026-08-31T11:58:23.400Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}