web/elements: fix error with blank SearchSelect elements in forms

closes #2469

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2022-03-11 20:36:54 +01:00
parent becec6b7d8
commit abc73deda0
3 changed files with 10 additions and 6 deletions

View File

@ -40,7 +40,7 @@ export class SearchSelect<T> extends LitElement {
renderElement!: (element: T) => string; renderElement!: (element: T) => string;
@property({ attribute: false }) @property({ attribute: false })
value!: (element: T) => unknown; value!: (element: T | undefined) => unknown;
@property({ attribute: false }) @property({ attribute: false })
selected!: (element: T) => boolean; selected!: (element: T) => boolean;

View File

@ -153,7 +153,11 @@ export class Form<T> extends LitElement {
json[element.name] = element.checked; json[element.name] = element.checked;
} else if (element.tagName.toLowerCase() === "ak-search-select") { } else if (element.tagName.toLowerCase() === "ak-search-select") {
const select = element as unknown as SearchSelect<unknown>; const select = element as unknown as SearchSelect<unknown>;
try {
json[element.name] = select.value(select.selectedObject); json[element.name] = select.value(select.selectedObject);
} catch {
console.debug("authentik/form: SearchSelect.value error");
}
} else { } else {
for (let v = 0; v < values.length; v++) { for (let v = 0; v < values.length; v++) {
this.serializeFieldRecursive(element, values[v], json); this.serializeFieldRecursive(element, values[v], json);

View File

@ -222,8 +222,8 @@ export class PolicyBindingForm extends ModelForm<PolicyBinding, string> {
.renderElement=${(group: Group): string => { .renderElement=${(group: Group): string => {
return group.name; return group.name;
}} }}
.value=${(group: Group): string => { .value=${(group: Group | undefined): string => {
return group.pk; return group ? group.pk : "";
}} }}
.selected=${(group: Group): boolean => { .selected=${(group: Group): boolean => {
return group.pk === this.instance?.group; return group.pk === this.instance?.group;
@ -256,8 +256,8 @@ export class PolicyBindingForm extends ModelForm<PolicyBinding, string> {
.renderElement=${(user: User): string => { .renderElement=${(user: User): string => {
return UserOption(user); return UserOption(user);
}} }}
.value=${(user: User): number => { .value=${(user: User | undefined): number => {
return user.pk; return user ? user.pk : 0;
}} }}
.selected=${(user: User): boolean => { .selected=${(user: User): boolean => {
return user.pk === this.instance?.user; return user.pk === this.instance?.user;