Forms
A Form step collects information from a person. This can be an
employee or someone outside the organisation.
Use role to specify who is responsible for the step and form to
define its fields.
Form example
Section titled “Form example”In this single-step process, the Sales role enters the SKU and quantity from the Web order form example.
import { NumberField, 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 webOrder = new Process(org, "web-order", { name: "Web order", purpose: "Capture an order from the website",})
const orderForm = Schema.Struct({ sku: TextField({ label: "SKU" }), quantity: NumberField({ label: "Quantity" }),})
const captureOrder = new Form(webOrder, "Capture order", { role: sales, form: orderForm,})
webOrder.start(captureOrder).end()Forms are Effect TS schemas
Section titled “Forms are Effect TS schemas”Note that forms are actually Effect TS Schemas.
const orderForm = Schema.Struct({ sku: TextField({ label: "SKU" }), quantity: NumberField({ label: "Quantity" }),})In Process Focus we start with the shape of the data we want to send to the API, then work backward to derive a user data entry form.
See Form components for common fields and ways to organise them.
Add validation
Section titled “Add validation”A form is a schema. Validation is therefore schema validation. Below we trim the SKU and make sure it is not blank. The quantity is required and must be a positive whole number. The process and role stay the same.
import { NumberFieldFrom, NumberFromString, TextFieldFrom } from "@pf/form-schema"
const orderForm = Schema.Struct({ sku: TextFieldFrom( Schema.Trim.pipe(Schema.check(Schema.isMinLength(1))), { label: "SKU" }, ), quantity: NumberFieldFrom( NumberFromString.pipe( Schema.check(Schema.isInt()), Schema.check(Schema.isGreaterThan(0)), ), { label: "Quantity", required: true }, ),})The Dashboard runs these field checks during data entry. The backend runs them again on submission.
Check stock on submission
Section titled “Check stock on submission”Use the Form step’s validate callback for schema checks that need
database access. The backend runs it after decoding the form fields,
so sku is already trimmed and quantity is a positive whole number.
The Dashboard does not run this callback during data entry.
Replace the captureOrder step above with this version, using the
validated orderForm. In this example, getAvailableStock is your
application’s database lookup. It returns an Effect that yields the
available quantity, or undefined if the SKU is unknown.
import { Effect } from "effect"import { getAvailableStock } from "./inventory"
const captureOrder = new Form(webOrder, "Capture order", { role: sales, form: orderForm, validate: ({ sku, quantity }) => Effect.gen(function* () { const available = yield* getAvailableStock(sku) if (available === undefined) return `Unknown SKU: ${sku}.` if (quantity > available) return `Only ${available} units of ${sku} are available.` return undefined }),})Return a message to reject the submission with a schema validation error,
or undefined to let the submission proceed.
Default values
Section titled “Default values”Set default on a field to prefill it when the form opens. For example,
start the order quantity at 1 so the person only needs to change it
when the customer orders more than one:
const orderForm = Schema.Struct({ sku: TextField({ label: "SKU" }), quantity: NumberField({ label: "Quantity", default: 1 }),})The default must match the field’s value type. Here, use the number 1,
not the string "1".
Form rules
Section titled “Form rules”Use a form rule to change a field’s behaviour based on what the person enters. For example, show and require a delivery address only when the order will be shipped:
import { BooleanField, TextArea } from "@pf/form-schema"import { withFormRules } from "@pf/form-submission-schema"import { Schema } from "effect"
const deliveryForm = withFormRules( Schema.Struct({ shipOrder: BooleanField({ label: "Ship this order", default: false }), deliveryAddress: Schema.optional(TextArea({ label: "Delivery address" })), }), ({ shipOrder }) => ({ deliveryAddress: { hidden: shipOrder !== true, required: shipOrder === true, }, }),)Schema.optional lets the address be absent for pickup, when the field is
hidden and not submitted. When the person selects Ship this order, the
rule shows the field and requires an address. The server checks the rule
again on submission.