diff --git a/api/js/etemplate/Et2Select/Et2Select.ts b/api/js/etemplate/Et2Select/Et2Select.ts index cf1ee44b8e..607b52f2f1 100644 --- a/api/js/etemplate/Et2Select/Et2Select.ts +++ b/api/js/etemplate/Et2Select/Et2Select.ts @@ -453,10 +453,13 @@ export class Et2Select extends Et2WithSearchMixin(Et2WidgetWithSelect) } // If no value is set, choose the first option // Only do this on once during initial setup, or it can be impossible to clear the value - const valueArray = Array.isArray(this.value) ? this.value : ( + let valueArray = Array.isArray(this.value) ? this.value : ( !this.value ? [] : (this.multiple ? this.value.toString().split(',') : [this.value]) ); + // Check for value using missing options (deleted or otherwise not allowed) + valueArray = this.filterOutMissingOptions(valueArray); + // value not in options --> use emptyLabel, if exists, or first option otherwise if(this.select_options.filter((option) => valueArray.find(val => val == option.value) || Array.isArray(option.value) && option.value.filter(o => valueArray.find(val => val == o.value))).length === 0) @@ -497,6 +500,29 @@ export class Et2Select extends Et2WithSearchMixin(Et2WidgetWithSelect) this.value = val || ''; } + /** + * Check a value for missing options and remove them. + * + * We'll warn about it in the helpText, and if they save the change will be made. + * This is to avoid the server-side validation error, which the user can't do much about. + * + * @param {string[]} value + * @returns {string[]} + */ + filterOutMissingOptions(value : string[]) : string[] + { + if(!this.readonly && value && value.length > 0 && !this.allowFreeEntries && this.select_options.length > 0) + { + const missing = value.filter(v => !this.select_options.some(option => option.value == v)); + if(missing.length > 0) + { + this.helpText = this.egw().lang("Invalid option '%1' removed", missing.join(", ")); + value = value.filter(item => missing.indexOf(item) == -1); + } + } + return value; + } + transformAttributes(attrs) { super.transformAttributes(attrs); diff --git a/api/js/etemplate/Et2Select/Et2SelectAccount.ts b/api/js/etemplate/Et2Select/Et2SelectAccount.ts index 0ab1361a9d..fc45fab108 100644 --- a/api/js/etemplate/Et2Select/Et2SelectAccount.ts +++ b/api/js/etemplate/Et2Select/Et2SelectAccount.ts @@ -115,6 +115,14 @@ export class Et2SelectAccount extends SelectAccountMixin(Et2StaticSelectMixin(Et super.select_options = new_options; } + /** + * Override filter to not, since we don't have all accounts available + */ + filterOutMissingOptions(value : string[]) : string[] + { + return value; + } + /** * Override icon for the select option *