Runtime Methods
setData and setMeta let you push state changes into a mounted form without re-initialising it. Use them to pre-fill fields after an async fetch, sync the form with external state, or toggle meta-driven behaviour at runtime. setHealth reports an error the form cannot see by itself, such as a server that cannot be reached.
setData and setMeta replace their respective state objects entirely: there is no deep merge.
In Lit and Angular, the form element exists before the form has started. A setData, setMeta or setHealth call in that time is kept. The last setData or setMeta call replaces the data or meta of the config, like a call after the start. A setHealth call is applied after the start: the first formHealth value is the health of the started form (ok, or an error of the start such as an error in the definition), and the health of the call comes next.
A new config starts a new form. Which form gets a call made at that time depends on the framework:
- Lit: a call after you assign the new
configand before the form starts with it goes to the new form. The old form gets nothing. - React: the form starts with the new config in an effect, and an object ref (
useRef) gets the handle of the new form there. So a call from auseEffectof the component that renders the form reaches the new form. A call from auseLayoutEffect, or through a handle that a callback ref stored, runs before the start and is lost. - Angular and Vue: a call before the form gets the new config goes to the old form, and then the new form replaces it.
Getting a reference to the form
Section titled “Getting a reference to the form”Each framework has its own mechanism for obtaining a handle to the form component instance:
import { useRef } from 'react';import { GuiForm } from '@golemui/gui-react';import type { FormComponentHandle } from '@golemui/react';
export function MyForm({ config }) { const formRef = useRef<FormComponentHandle>(null);
return <GuiForm ref={formRef} config={config} />;}import { Component, ViewChild } from '@angular/core';import { FormComponent } from '@golemui/gui-angular';
@Component({ imports: [FormComponent], selector: 'app-my-form', template: `<gui-form #myForm [config]="config"></gui-form>`,})export class MyFormComponent { @ViewChild('myForm') form!: FormComponent;
protected config = { /* ... */ };}import { LitElement, html } from 'lit';import { customElement, query } from 'lit/decorators.js';import { FormElement } from '@golemui/gui-lit';
@customElement('my-app')export class MyApp extends LitElement { @query('gui-form') private form!: FormElement;
override createRenderRoot() { return this; }
override render() { return html`<gui-form .config=${this.config}></gui-form>`; }}<script setup lang="ts">import { ref } from 'vue';import { GuiForm, type GuiFormHandle } from '@golemui/gui-vue';
const formRef = ref<GuiFormHandle | null>(null);const config = { /* ... */ };</script>
<template> <GuiForm ref="formRef" :config="config" /></template>import '@golemui/gui-components/index.css';import '@golemui/gui-lit';
const form = document.getElementById('app-form');form.config = { /* ... */ };// `form.setData(...)`, `form.setMeta(...)` and `form.setHealth(...)` are available directly on the element.setData
Section titled “setData”Replaces the form’s current data state. All field values are overwritten with the supplied object.
import { useRef } from 'react';import { GuiForm } from '@golemui/gui-react';import type { FormComponentHandle } from '@golemui/react';
export function MyForm({ config }) { const formRef = useRef<FormComponentHandle>(null);
async function loadUser(userId: string) { const user = await fetchUser(userId); formRef.current?.setData({ name: user.name, email: user.email }); }
return ( <> <button onClick={() => loadUser('42')}>Load user</button> <GuiForm ref={formRef} config={config} /> </> );}import { Component, ViewChild } from '@angular/core';import { FormComponent } from '@golemui/gui-angular';
@Component({ imports: [FormComponent], selector: 'app-my-form', template: ` <button (click)="loadUser('42')">Load user</button> <gui-form #myForm [config]="config"></gui-form> `,})export class MyFormComponent { @ViewChild('myForm') form!: FormComponent;
protected config = { /* ... */ };
async loadUser(userId: string) { const user = await this.userService.fetch(userId); this.form.setData({ name: user.name, email: user.email }); }}import { LitElement, html } from 'lit';import { customElement, query } from 'lit/decorators.js';import { FormElement } from '@golemui/gui-lit';
@customElement('my-app')export class MyApp extends LitElement { @query('gui-form') private form!: FormElement;
override createRenderRoot() { return this; }
async loadUser(userId: string) { const user = await fetchUser(userId); this.form.setData({ name: user.name, email: user.email }); }
override render() { return html` <button @click=${() => this.loadUser('42')}>Load user</button> <gui-form .config=${this.config}></gui-form> `; }}<script setup lang="ts">import { ref } from 'vue';import { GuiForm, type GuiFormHandle } from '@golemui/gui-vue';
const formRef = ref<GuiFormHandle | null>(null);const config = { /* ... */ };
async function loadUser(userId: string) { const user = await fetchUser(userId); formRef.value?.setData({ name: user.name, email: user.email });}</script>
<template> <button @click="loadUser('42')">Load user</button> <GuiForm ref="formRef" :config="config" /></template><button id="load-btn">Load user</button><gui-form id="app-form"></gui-form>import '@golemui/gui-components/index.css';import '@golemui/gui-lit';
const form = document.getElementById('app-form');form.config = { /* ... */ };
async function loadUser(userId) { const user = await fetchUser(userId); form.setData({ name: user.name, email: user.email });}
document.getElementById('load-btn').addEventListener('click', () => loadUser('42'));setMeta
Section titled “setMeta”Replaces the form’s current meta state. Meta values are available inside the form definition for template interpolation ({{$meta.key}}) and conditions ($meta.key === true).
import { useRef } from 'react';import { gui } from '@golemui/gui-shared';import { GuiForm } from '@golemui/gui-react';import type { FormComponentHandle } from '@golemui/react';
const formDef = [ gui.displays.alert('status-banner', { text: 'Connection: {{$meta.status}}', }),];
const config = { formDef, meta: { status: 'offline' } };
export function MyForm() { const formRef = useRef<FormComponentHandle>(null);
function goOnline() { formRef.current?.setMeta({ status: 'online' }); }
return ( <> <button onClick={goOnline}>Connect</button> <GuiForm ref={formRef} config={config} /> </> );}import { Component, ViewChild } from '@angular/core';import { gui } from '@golemui/gui-shared';import { FormComponent } from '@golemui/gui-angular';
@Component({ imports: [FormComponent], selector: 'app-my-form', template: ` <button (click)="goOnline()">Connect</button> <gui-form #myForm [config]="config"></gui-form> `,})export class MyFormComponent { @ViewChild('myForm') form!: FormComponent;
protected config = { formDef: [ gui.displays.alert('status-banner', { text: 'Connection: {{$meta.status}}', }), ], meta: { status: 'offline' }, };
goOnline() { this.form.setMeta({ status: 'online' }); }}import { LitElement, html } from 'lit';import { customElement, query } from 'lit/decorators.js';import { gui } from '@golemui/gui-shared';import { FormElement } from '@golemui/gui-lit';
@customElement('my-app')export class MyApp extends LitElement { @query('gui-form') private form!: FormElement;
config = { formDef: [ gui.displays.alert('status-banner', { text: 'Connection: {{$meta.status}}', }), ], meta: { status: 'offline' }, };
override createRenderRoot() { return this; }
goOnline() { this.form.setMeta({ status: 'online' }); }
override render() { return html` <button @click=${() => this.goOnline()}>Connect</button> <gui-form .config=${this.config}></gui-form> `; }}<script setup lang="ts">import { ref } from 'vue';import { gui } from '@golemui/gui-shared';import { GuiForm, type GuiFormHandle } from '@golemui/gui-vue';
const formRef = ref<GuiFormHandle | null>(null);
const config = { formDef: [ gui.displays.alert('status-banner', { text: 'Connection: {{$meta.status}}', }), ], meta: { status: 'offline' },};
function goOnline() { formRef.value?.setMeta({ status: 'online' });}</script>
<template> <button @click="goOnline">Connect</button> <GuiForm ref="formRef" :config="config" /></template><button id="connect-btn">Connect</button><gui-form id="app-form"></gui-form>import '@golemui/gui-components/index.css';import '@golemui/gui-lit';import { gui } from '@golemui/gui-shared';
const form = document.getElementById('app-form');form.config = { formDef: [ gui.displays.alert('status-banner', { text: 'Connection: {{$meta.status}}', }), ], meta: { status: 'offline' },};
function goOnline() { form.setMeta({ status: 'online' });}
document.getElementById('connect-btn').addEventListener('click', goOnline);API reference
Section titled “API reference”| Method | Signature | Description |
|---|---|---|
setData | (data: Record<string, any>) => void | Replaces the form’s data state. All field values are overwritten. |
setMeta | (meta: Record<string, any>) => void | Replaces the form’s meta state. Used in template interpolations and conditions. |
See also
Section titled “See also”- Features / Form Events — reacting to widget events emitted by the form.
- Form Definition API / Runtime Functions — reactive props computed from live form state.
setHealth
Section titled “setHealth”Sets the form health from outside, next to setData and setMeta. Use it for an error the form cannot see by itself, for example a form that saves each change to a server that goes down.
setHealth({ status: 'errored', code, message })shows the error like one of the form’s own: the form sendsformHealth, shows theformHealthBoundaryand stops updating. AsetDatacall does not change the inputs while it is frozen.setHealth({ status: 'ok' })hides the boundary, sendsformHealthagain, and the user can type again. Data written withsetDatawhile the form was frozen shows with the next change.
import { useRef } from 'react';import { GuiForm } from '@golemui/gui-react';import type { FormComponentHandle } from '@golemui/react';
export function MyForm({ config }) { const formRef = useRef<FormComponentHandle>(null);
function serverDown() { formRef.current?.setHealth({ status: 'errored', code: 10_001, message: 'The server cannot be reached', }); }
function serverBack() { formRef.current?.setHealth({ status: 'ok' }); }
return <GuiForm ref={formRef} config={config} />;}import { Component, ViewChild } from '@angular/core';import { FormComponent } from '@golemui/gui-angular';
@Component({ imports: [FormComponent], selector: 'app-my-form', template: `<gui-form #myForm [config]="config"></gui-form>`,})export class MyFormComponent { @ViewChild('myForm') form!: FormComponent;
protected config = { /* ... */ };
serverDown() { this.form.setHealth({ status: 'errored', code: 10_001, message: 'The server cannot be reached', }); }
serverBack() { this.form.setHealth({ status: 'ok' }); }}import { LitElement, html } from 'lit';import { customElement, query } from 'lit/decorators.js';import { FormElement } from '@golemui/gui-lit';
@customElement('my-app')export class MyApp extends LitElement { @query('gui-form') private form!: FormElement;
override createRenderRoot() { return this; }
serverDown() { this.form.setHealth({ status: 'errored', code: 10_001, message: 'The server cannot be reached', }); }
serverBack() { this.form.setHealth({ status: 'ok' }); }
override render() { return html`<gui-form .config=${this.config}></gui-form>`; }}<script setup lang="ts">import { ref } from 'vue';import { GuiForm, type GuiFormHandle } from '@golemui/gui-vue';
const formRef = ref<GuiFormHandle | null>(null);const config = { /* ... */ };
function serverDown() { formRef.value?.setHealth({ status: 'errored', code: 10_001, message: 'The server cannot be reached', });}
function serverBack() { formRef.value?.setHealth({ status: 'ok' });}</script>
<template> <GuiForm ref="formRef" :config="config" /></template>import '@golemui/gui-components/index.css';import '@golemui/gui-lit';
const form = document.getElementById('app-form');form.config = { /* ... */ };
window.addEventListener('offline', () => form.setHealth({ status: 'errored', code: 10_001, message: 'You are offline: your changes cannot be saved', }),);window.addEventListener('online', () => form.setHealth({ status: 'ok' }));