Skip to content

Using process state in forms

A submitted form adds its output to the execution’s process state. A later form can use those values to prefill fields without asking the person to enter the same information again.

The first form captures the SKU. The review form displays that SKU as read-only and collects a separate approval decision. This first example shows the server-side declarations; it omits the browser entrypoint each form needs to appear in the Dashboard.

import { BooleanField, TextField } from "@pf/form-schema"
import { Form, Organisation, Process, Role } from "@pf/process"
import { Schema } from "effect"
export const org = new Organisation({ name: "Demo Store" })
const sales = new Role(org, "Sales")
const reviewer = new Role(org, "Reviewer")
const webOrder = new Process(org, "web-order", {
name: "Web order",
purpose: "Capture and review a web order",
})
const captureOrder = new Form(webOrder, "Capture order", {
role: sales,
form: Schema.Struct({
sku: TextField({ label: "SKU" }),
}),
})
const afterCapture = webOrder.start(captureOrder)
const reviewOrder = new Form(afterCapture, "Review order", {
role: reviewer,
form: ({ value }) => ({
capturedSku: TextField({
label: "Submitted SKU",
default: value((state) => state.sku),
readOnly: true,
}),
approved: BooleanField({ label: "Approve this order", default: false }),
}),
})
afterCapture.next(reviewOrder).end()

Pass afterCapture to the second Form so TypeScript knows that sku is available on that path. The value helper reads the SKU from the current execution when the review form is prepared. Submitting the review adds approved to process state; the read-only capturedSku field is not submitted, and the original sku stays unchanged.

Set defaults in a shared browser-safe schema

Section titled “Set defaults in a shared browser-safe schema”

Every form displayed in a browser needs a browser-safe schema. This applies to Dashboard forms as well as embedded start forms. The schema defines the fields, validation rules, and any fixed defaults. The form’s browser entrypoint tells the frontend where to load that schema. See Forms in Browser for how to package the schema and keep process state on the server.

The inline default: value(...) above reads process state on the server. To use the same schema module on the server and in the browser, keep that state-dependent default in the form’s bindings. The server reads the current execution’s state and supplies the field value without exposing the full process state to the browser.

For example, put the review form’s shared schema in src/review-order-form.ts:

import { BooleanField, TextField } from "@pf/form-schema"
import { Schema } from "effect"
export const reviewOrderForm = Schema.Struct({
capturedSku: TextField({ label: "Submitted SKU", readOnly: true }),
approved: BooleanField({ label: "Approve this order", default: false }),
})

Replace the inline review form above with this version:

import { reviewOrderForm } from "./review-order-form"
const reviewOrder = new Form(afterCapture, "Review order", {
role: reviewer,
form: reviewOrderForm,
browser: {
entrypoint: "src/review-order-form.ts",
exportName: "reviewOrderForm",
},
bindings: (state) => ({
capturedSku: { default: state.sku },
}),
})

The approval checkbox starts unchecked for every review, so its default can stay in the shared schema. The SKU varies by execution, so the binding supplies it when the server prepares the form.

For comparison, the inline form sets those same defaults directly on the fields:

form: ({ value }) => ({
capturedSku: TextField({
label: "Submitted SKU",
readOnly: true,
default: value((state) => state.sku),
}),
approved: BooleanField({ label: "Approve this order", default: false }),
}),

Both versions supply the same defaults. An inline default changes how the server supplies the SKU; the browser still needs a schema to render the field.