Skip to content

Forms in Browser

Every form a person fills out needs a schema the browser can load. This is true for a Support task in the Dashboard as well as a form embedded on a website. Browser-safe describes the code in the schema. It says nothing about who is allowed to open the form.

The browser uses the schema to render fields and check what the person enters. The server uses the same schema to validate the submission before accepting it. Only the schema needs to run in both places. The process definition, authorisation checks, and database access stay on the server.

Suppose a customer has submitted a support ticket. A Support team member needs to see the reported issue and tick Done when it is resolved. Export the fields for that step from src/resolve-ticket-form.ts:

import { BooleanField, TextArea } from "@pf/form-schema"
import { Schema } from "effect"
export const resolveTicketForm = Schema.Struct({
reportedIssue: TextArea({ label: "Reported issue", readOnly: true }),
done: BooleanField({ label: "Done", default: false, required: true }),
})

The schema specifies the same fields for every ticket. Done starts unchecked, and the required checkbox must be checked to submit. Keep this module and its imports safe to send to the browser: include field definitions and validation rules, but leave out credentials and server-only services.

In the process module, import the schema and use it as the form’s form property. Set browser.entrypoint to its file path relative to the organisation project root, and browser.exportName to its named export. pfcli build uses these settings to package the schema for the frontend. Importing the schema on the server alone does not make it available to the browser.

Here afterSubmission is the flow after the visitor submits the issue, and support is the role responsible for resolving it:

import { Form } from "@pf/process"
import { resolveTicketForm } from "./resolve-ticket-form"
const resolveTicket = new Form(afterSubmission, "resolve-ticket", {
role: support,
form: resolveTicketForm,
browser: {
entrypoint: "src/resolve-ticket-form.ts",
exportName: "resolveTicketForm",
},
bindings: (state) => ({
reportedIssue: { default: state.issue },
}),
})
afterSubmission.next(resolveTicket).end()

Done starts unchecked for every ticket, so default: false belongs in the shared schema. Reported issue is different for each ticket. You cannot write default: state.issue in the exported Schema.Struct: there is no current ticket or state variable when that module is loaded.

bindings still sets a default, but it does so when the server prepares the form for a particular ticket. The browser receives the issue text to display without needing access to the rest of the ticket’s process state.

An inline form declaration can also compute a default with default: value((state) => state.issue). That callback runs on the server. It changes how the default is supplied; the form still needs a browser entrypoint to render. See Using process state in forms for a comparison, or Embedded forms for the full support ticket process.