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;
@property({ attribute: false })
value!: (element: T) => unknown;
value!: (element: T | undefined) => unknown;
@property({ attribute: false })
selected!: (element: T) => boolean;

View File

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

View File

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