Et2Select: Remove values that use options that aren't there to avoid the validation error

This commit is contained in:
nathan 2023-05-05 11:08:47 -06:00
parent a8e83ad59f
commit 4ce6bb3f3d
2 changed files with 35 additions and 1 deletions

View File

@ -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);

View File

@ -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
*