Allow config flow to show error per field (#19522)

This commit is contained in:
Bram Kragten 2024-01-30 12:24:45 +01:00 committed by GitHub
parent 86bbff36ea
commit 107f0da88b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 35 additions and 4 deletions

View File

@ -154,6 +154,10 @@ class HaAlert extends LitElement {
.issue-type.success::after {
background-color: var(--success-color);
}
:host ::slotted(ul) {
margin: 0;
padding-inline-start: 20px;
}
`;
}

View File

@ -45,7 +45,10 @@ export class HaForm extends LitElement implements HaFormElement {
@property({ attribute: false }) public schema!: readonly HaFormSchema[];
@property({ attribute: false }) public error?: Record<string, string>;
@property({ attribute: false }) public error?: Record<
string,
string | string[]
>;
@property({ attribute: false }) public warning?: Record<string, string>;
@ -228,7 +231,20 @@ export class HaForm extends LitElement implements HaFormElement {
return this.computeHelper ? this.computeHelper(schema) : "";
}
private _computeError(error, schema: HaFormSchema | readonly HaFormSchema[]) {
private _computeError(
error: string | string[],
schema: HaFormSchema | readonly HaFormSchema[]
): string | TemplateResult {
if (Array.isArray(error)) {
return html`<ul>
${error.map(
(err) =>
html`<li>
${this.computeError ? this.computeError(err, schema) : err}
</li>`
)}
</ul>`;
}
return this.computeError ? this.computeError(error, schema) : error;
}

View File

@ -201,8 +201,19 @@ class StepFlowForm extends LitElement {
step,
});
} catch (err: any) {
this._errorMsg =
(err && err.body && err.body.message) || "Unknown error occurred";
if (err && err.body) {
if (err.body.message) {
this._errorMsg = err.body.message;
}
if (err.body.errors) {
this.step = { ...this.step, errors: err.body.errors };
}
if (!err.body.message && !err.body.errors) {
this._errorMsg = "Unknown error occurred";
}
} else {
this._errorMsg = "Unknown error occurred";
}
} finally {
this._loading = false;
}