Events and operations
Event handlers let a schema react to what happens in a form, such as a record loading or a field changing. A handler never changes the form directly. It describes what should happen as a list of operations, and the renderer or your application applies them. This page explains how handlers are registered, what they can read, and how their operations are applied.
For which events the official renderers dispatch, see Event dispatch in official bindings.
Registering handlers
The code in form.events.code runs once, when the engine is created. Each ON call in it
registers a handler, and each OFF call removes one:
Code
- Official bindings identify the field by its
data_name, so handlers for binding-dispatched field events must also register withdata_name. Core matches the registration string exactly and does not translate betweenkeyanddata_name. - Leave out the field to run the handler for every field.
- An unknown event type prints a warning, and the handler is not registered.
ONandOFFinside a handler take effect after the dispatch succeeds. If the dispatch is rejected, they do nothing. Callbacks nested inside a handler work on Hermes, but they cannot use the outer handler's local variables.
How handler code runs
The engine keeps the source code of every handler defined in form.events.code and runs that code
each time the event is dispatched. A handler therefore cannot use variables from the code around it:
constants, variables, and functions declared at the top level of form.events.code are not
available inside the handler.
Code
Inside a handler, read form values with $data_name. For logic shared by several handlers, pass a
function in the engine's helpers option.
Handlers must be synchronous. If a handler is async or returns a promise, the engine warns about
it, and trigger() returns only the operations the handler created before its first await.
Operations created after that are discarded with a warning, and an error thrown after an await is
reported instead of escaping the engine. These warnings print to the console in development and
reach any WarningSystem handlers.
Because the engine keeps the source code itself, handlers also run on Hermes, the JavaScript engine
of React Native, which does not expose the source of a function. This works for function
declarations, function expressions, and arrow functions written in form.events.code, including
those assigned to a variable and passed to ON later.
For any other callback, the engine reads the source with toString(). This includes callbacks that
come from helpers, object methods, getters and setters, and class
members. That works in Node.js and browsers, but not on Hermes. There, the engine prints
Event callback source is unavailable and runs the remaining handlers.
When handlers run
A handler runs only when its event is dispatched, either by a binding or by your application with
engine.trigger(eventType, fieldDataName, metadata). Creating the engine or running eval() does
not dispatch events.
For one dispatch, the handlers registered for that field run first, in the order they were
registered. Handlers registered for every field run after them. If a handler throws, the engine
prints [form0] Expression evaluation failed to the console and the remaining handlers still run.
What a handler can read
A handler reads field values with $data_name, like a calculation. It sees the values at the moment
the event was dispatched. For a change event from an official renderer, that is after the
evaluation pass that followed the edit, so calculated fields are already up to date.
Which fields a handler can read depends on the event:
- Record events, such as
load-recordandedit-record, can read only main-form fields. - Field events, such as
change, follow the same rules as a calculation defined on the field that triggered them. See Parent and child scopes.
Each handler also receives an event object with type, fieldKey (the field's data_name), and
timestamp, plus any metadata passed to trigger(). The official renderers add the new value
and the field definition to change events.
Handlers can use the shared expression builtins, such as IF and CHOICEVALUE, in addition to the
event builtins. Calculation-only builtins such as SETRESULT are not available.
SETVALUE does not update what the handler reads
SETVALUE only records an operation. For the rest of the handler, $data_name still returns the
value from when the event was dispatched. The new value is visible after the operation is applied.
Operations
SETVALUE and ALERT each add one operation to the list that engine.trigger() returns:
Code
The operations from all handlers of one dispatch are returned together, in the order the handlers ran.
Operations are checked before they are returned
Before trigger() returns, the engine checks every SETVALUE against the handler's scope. If one
targets a field the handler cannot write, or a field that is not in the schema, the engine reports a
warning, and trigger() returns no operations for that event. Every SETVALUE that trigger()
returns targets a field the handler is allowed to change.
How the official bindings apply operations
By default, the official renderers:
- show each
ALERTin their own alert dialog; and - write each
SETVALUEvalue into the form and run a new evaluation pass.
Choice fields take string IDs
SETVALUE takes a string ID for a SingleChoiceField or BooleanField, and an array of string
IDs for a MultiChoiceField. If a value has the wrong shape, none of that event's operations are
applied, and the renderer logs Event failed. to the console. See the SETVALUE
reference.
The React and React Native FormRenderer components let an application handle non-alert operations
through engineOptions.onOperations. Each renderer owns ALERT presentation and passes the other
operations to your callback.
Code
Call applyDefault with any operations you want the binding to handle as usual.
Applying operations without a renderer
If you use form0-core directly, apply the returned list yourself. For SETVALUE, call
engine.setValue(fieldDataName, valueToSet), which accepts a string ID for a choice field and runs
the evaluation. See Evaluation cycle.
No chains of change events
Applying a SETVALUE runs a new evaluation pass, but it does not dispatch change for the field
it set. A change handler for age that sets adult_note does not run the change handlers for
adult_note.
If setting one field should also trigger the logic of another, put both in the same handler, or use
a CalculatedField when the value can be derived from other fields.
Get detailed results
engine.trigger() returns only the operations. Use
engine.triggerDetailed(eventType, fieldKey, metadata) when you also need validation messages and
failures. It is synchronous, and it returns three arrays:
Code
operations: what to apply. If a handler fails, this is empty.validationIssues: messages fromINVALID().issues: failures while handlers ran, each withcode,message,eventType, andinstancePath.
Neither method evaluates the form, saves a record, or closes an editor.
Run a row's handlers
To run the handlers of a repeatable section row without
a renderer, pass eventScope.instancePath when you create the engine. The main record's path is
[]. Each row segment has repeatableKey, instanceId, index, and identity. Paths use schema
keys, and listeners use data_name. A row without an ID uses identity: "position". form0 checks
the path against the schema, but your application must check that the row still exists.
A row handler can read its own fields, and the parent-row and main-form fields your application
supplies. SETVALUE() writes only to the current row. Sibling rows, child rows, and repeatable
collections are out of reach. Metadata cannot replace the event's own properties, such as its type
or scope.
What each kind of handler can do
| Handlers | Can | Cannot |
|---|---|---|
validate-record, validate-repeatable | Call INVALID(message) | Change values, show alerts, or return promises |
cancel-record, cancel-repeatable | Read values | Change values, call INVALID(), or show alerts |
unload-record, unload-repeatable | Read values | Change values, call INVALID(), or show alerts |
save-record and save-repeatable run before a save. There are no saved-* schema events.
Applications own saving, success messages, and closing the editor.
Event metadata can contain plain objects, arrays, dates, simple values, and File or Blob
handles, which keep their metadata and content. Other object types are rejected. Schema handlers
cannot be asynchronous.
See the maintained event contract for the complete rules.
Read-only records
Each handler receives event.mode, which is "readonly" or "edit" for the record it runs in. A
read-only row inside an editable record receives "readonly". The mode is separate from whether the
record is new, whether this is a draft, and the read_only setting of a field. Metadata and
handlers cannot change it.
load-record runs once each time a record opens, in either mode. A record opened for editing runs
new-record or edit-record after it. Switching from read-only to editing runs edit-record
without running load-record again.
In a read-only record, handlers can read values, show an ALERT, and add or remove listeners. A
SETVALUE rejects the whole batch, including any alerts and listener changes, and the record stays
readable. triggerDetailed() reports this in issues, and trigger() returns no operations and
reports a warning.
Put setup that must run whenever editing begins in a new-record or edit-record handler, not in
load-record. Helpers your application passes to the engine can have their own side effects, which
form0 cannot undo.