Skip to main content
Install, init, and walkthrough examples live in Overview & setup. This page is the reference for initForms / initFormsServer, the form instance methods, the React hooks, and the analytics events Forms emits. Which fields a form reads and writes depends on its schema.

Set up & use forms

In the browser, call initForms; on the server, call initFormsServer. Each checks your Core setup once, then lets you open a form with getForm.
InitFormsOptions
required
Init configuration. core is required; the rest are optional. See Options below.
string
required
Passed to getForm. Must be the id of a schema registered on Core. An unknown id throws a FormsError. getForm returns one instance per form id — the first call builds it, every later call returns that same instance.

Options

Passed to initForms. Per-form options (customValidations, serverFormData) are arrays keyed by formId.
EmbeddablesInstance
required
An initialized @embeddables/core instance with at least one form schema registered (via config.forms from em build, or forms on initEmbeddables). Init throws a FormsError if Core is missing required methods or has no form schemas. Forms composes on Core for identity but never reads or writes the identity entry.
{ formId, customValidations }[]
Per-field validator functions, matched to schemas by formId. Use this for CLI-generated schemas, which cannot carry functions themselves (a schema must survive a JSON round trip; only validations.custom may be a function). Each validator receives { value, values } and returns a string, a list of strings, or null. An unknown formId or field key throws a SchemaError.
{ formId, serverFormData }[]
Per-form values used to seed the form on init — typically the output of initFormsServer().getServerFormData(). On overlapping keys, serverFormData overrides localStorage; localStorage fills only the gaps. An unknown formId throws a SchemaError.
AnalyticsInstance
Optional analytics client used only for event tracking. When provided, a successful .set() emits data:updated plus one field:updated per changed field, and .submit() emits form:submitted — see Analytics events. A rejected trackEvent() never fails a .set() / .submit(); the failure surfaces as trackError on the result. In React, the CLI wires Analytics into Forms automatically from Core — pass analyticsInstance only to override that default. Omitting it disables event tracking; durable saves still run when Core resolves persistence config.

Methods and hooks

React covers the forms() module factory and the hooks — register the factory on EmbeddablesProvider before calling hooks. JavaScript & Server lists methods on the form from initForms or initFormsServer — the same names on both entry points; SSR uses in-memory storage only (no localStorage, durable save, or Analytics on .set() / .submit()). After set, submit, or validate, check result.ok and result.errors — a failed validation does not throw an error.
React bindings from @embeddables/forms/react. Register Forms on EmbeddablesProvider’s modules prop with the forms() module factory, then bind fields with these hooks. Every hook returns null / empty state until Forms is ready — handle that before calling instance methods.
Returns an EmbeddablesReactModule for the provider’s modules prop. The generated CLI setup wires this for you; pass options only to override. core is supplied by the provider — do not pass it here. Form schemas must be on config (from em build), not on forms().
{ formId, customValidations }[]
Same as customValidations on initForms.
{ formId, serverFormData }[]
Same as serverFormData on initForms — typically SSR hydration seed.
AnalyticsInstance
Optional analytics client for auto-emitted form events. The generated React setup wires Analytics automatically when both SDKs are installed.
string
required
Id of a declared form. No type arguments needed — the id narrows the form and the schema map comes from the registry em build augments.
FormInstance | null
The shared instance for this form id, or null until Forms is ready. Every useForm with the same id shares one instance.
Partial<FormValues>
Reactive stored values.
FieldErrors
Reactive per-field validation messages.
Typing updates local draft state via setValue; form.set() runs on onBlur (and only when the draft changed). Wire the input’s onChange to setValue and its onBlur to onBlur.
FormInstance | null
required
The instance from useForm (may be null).
FormFieldKey
required
The field key to bind.
FormInstance | null
required
The instance from useForm. Returns the reactive FieldErrors map.

Analytics events

Emitted automatically only when an analyticsInstance is wired (directly, or via the CLI in React). Each is a standard Analytics event — see the Analytics event reference for the full field list.
When Analytics is wired, do not also call analytics.trackEvent(...) yourself for these events — you will double-count. Forms owns data:updated, field:updated, and form:submitted for the forms it manages.
object
One entry per changed key; each has value (stringified, max 1024 chars) and label (the field’s label, max 256 chars).
string
The changed field’s key.
string
The field’s declared type (text, email, number, boolean, select, multiselect, json).
string | number | boolean | object | array
The raw new value.
string
Included only when the field declares a registryId / protocolFieldId.
string
The submitted form’s id. Answers are sent as separate data:updated / field:updated events, not on this event.

Returns

getForm returns a FormInstance. Its write methods resolve to result objects that never reject on validation failure.
boolean
required
true when the operation succeeded. set, submit, and validate all return it.
FieldErrors
required
Per-field validation messages; an empty object means no errors.
Partial<FormValues>
Present on submit and validate results — the current stored values.
unknown
Present on set / submit results only when an analyticsInstance was configured and its trackEvent rejected. The field values are still persisted; the error is reported, never thrown.