f365.events, form-level event bus

Subscribe to form lifecycle events, onLoad, onChange, onBeforeSave, onSave, onSaveError, onValidate, onCancel, onModeChange, onPageChange.

The f365.events namespace is a typed event bus for form-level lifecycle hooks. Subscribe in the form's Code drawer; the runtime cleans up on unmount automatically.

Event reference

PropertyDescription
onLoad(fn)Fires once after the form mounts and the schema is reconciled.
onChange(field, fn)Fires when the named field changes. Form-level alternative to per-field field(name).on("change"...).
onBeforeSave(fn)Fires before the save flow writes. Return { cancel: true } to abort.
onSave(fn)Fires after a successful save with the item id.
onSaveError(fn)Fires when the save flow throws. Receives the error and the partial payload.
onValidate(fn)Fires during the validation phase with the whole form payload. Return a string or an array of field-keyed messages to mark invalid.
onCancel(fn)Fires when the user clicks Cancel.
onModeChange(fn)Fires when the form switches between new / edit / display modes.
onPageChange(fn)Fires when the user switches tabs or accordion panels.

Examples

Audit trail on save

f365.events.onBeforeSave(() => {
  f365.field('LastModifiedBy').value = f365.user.current().email;
  f365.field('LastModifiedOn').value = new Date().toISOString();
});

Confirmation prompt

f365.events.onBeforeSave(async () => {
  if (Number(f365.field('Amount').value) > 50_000) {
    const ok = await f365.ui.showConfirm(
      'You are submitting over $50,000. This will require board approval. Continue?'
    );
    if (!ok) return { cancel: true };
  }
});

Redirect after save

f365.events.onSave(({ itemId }) => {
  f365.nav.redirectAfterSave(`/Lists/Orders/DispForm.aspx?ID=${itemId}`);
});

Form-level cross-field validation

f365.events.onValidate((payload) => {
  if (payload.StartDate && payload.EndDate && payload.EndDate < payload.StartDate) {
    return { EndDate: 'End date must be after start date.' };
  }
});

Subscription lifecycle

Each on* call returns an unsubscribe function. You rarely need to call it, the runtime cleans up automatically on form unmount, but it's available when you want to subscribe and unsubscribe dynamically:

const off = f365.events.onChange('SkuLookup', handler);
// later:
off();

Related articles