Form / foldkit-form
form
Every web form repeats the same plumbing by hand: storing unfinished text or checkbox drafts separately from decoded numbers and IDs, deciding whether to validate on input, blur, or submit, tracking whether the user changed anything since loading, and guarding against double submits or stale async checks.
foldkit-form builds a form's state, validation, and control metadata directly
from the Effect Schema.Struct your submit handler already accepts. You pass an
operation's input schema (Entity.input or Form.input), and you get back a
foldkit-bundle Bundle whose Model holds one
foldkit/fieldValidation field per key, plus typed controls for your view.
It is headless; foldkit-mixins-form renders those
controls with accessible styling, or you can render controls yourself.
See a live form editing entities in examples/entity
and at foldkit-chirp-demo.pages.dev.
What the form owns and what your app owns
| Fact | Owner |
|---|---|
| Valid input shape and constraints | Your input Schema.Struct |
| Meaning of each key (field or relation ID) | Entity.input |
| Drafts, validation state, and dirty tracking | foldkit-form, inside the parent Model |
| What happens on submit | Your parent update (mutation, sync op, or local state) |
| In-flight save state and server errors | Whoever runs the save (foldkit-crud or your app) |
| Options for a relation picker | Your application's query |
How drafts become decoded values
A draft is what the control holds while the user is typing — raw text such
as "4." for a number, or "" for an unchosen relation — which stays separate
from the typed submission value.
Form.make inspects your input schema and builds:
controlsandcontrolsByKey: each field's kind, label, description, required flag, and Entity metadata for your view.bundle: a Foldkit Bundle whoseupdatehandlesChanged,Blurred,Toggled,Submitted,Reset,Validated, andValidatedAll. On a validSubmitted, the bundle emits{ _tag: 'Submitted', value }with the decoded input.
Install
pnpm add effect foldkit foldkit-bundle foldkit-entity foldkit-formA first form
This defines a Post entity with a required title, builds a Rename form,
and runs two edits through its update — first triggering a validation error,
then producing a decoded submission value:
import { Schema } from 'effect'
import { Entity } from 'foldkit-entity'
import { Form } from 'foldkit-form'
const Post = Entity.define(
'Post',
Schema.Struct({
id: Schema.String,
title: Schema.String.check(Schema.isMinLength(1, { message: 'Enter a title' })).annotate({ title: 'Title' }),
}),
)
const RenameInput = Schema.Struct({ id: Schema.String, title: Post.fields.title.schema })
const Rename = Form.make('Rename', Entity.input(Post, RenameInput))
// 1. Typing an empty title fails the field check immediately.
const empty = Rename.bundle.update(
Rename.initial,
Rename.Message.Changed({ key: 'title', value: '' }),
undefined,
)
Rename.field(empty.model, 'title') // { _tag: 'Invalid', value: '', errors: ['Enter a title'] }
// 2. Fixing the title and submitting decodes a typed RenameInput.
const typed = Rename.bundle.update(empty.model, Rename.Message.Changed({ key: 'title', value: 'Hello' }), undefined)
const submitted = Rename.bundle.update(typed.model, Rename.Message.Submitted(), undefined)
submitted.outMessage // { _tag: 'Submitted', value: { id: '', title: 'Hello' } }Placing the form in a page
Compose Rename.bundle into your page with Bundle.withChild. onOut is
required so submitted values are always handled:
import { Bundle } from 'foldkit-bundle'
const Page = Bundle.compose({ saved: Schema.Array(RenameInput) }).pipe(
Bundle.withChild('rename', Rename.bundle, {
onOut: submitted => model => ({
model: { ...model, saved: [...model.saved, submitted.value] },
}),
}),
)
const RenameForm = Page.children.rename
const { placements } = Page
const update = placements.update(model => ({ model }))
let model = placements.initial({ saved: [] }).model
model = update(model, Page.Message.GotRenameMessage({
message: Rename.Message.Changed({ key: 'title', value: 'Hello' }),
})).model
model = update(model, Page.Message.GotRenameMessage({
message: Rename.Message.Submitted(),
})).model
// model.saved now holds [{ id: '', title: 'Hello' }]- For forms without an Entity (search, login, sign-up), pass
Form.input(schema):Form.make('Search', Form.input(SearchInput)). Rename.MessageprovidesChanged({ key, value }),Blurred({ key }),Submitted(),Reset(),Validated({ key }),ValidatedAll(),Toggled({ key })/Toggled({ key, choice })(applied to the current draft on click),FollowedIssue({ id }), andAskedForMore({ key }).Rename.controlsandRename.controlsByKeydescribe each field for views;Rename.field(model.rename, key)andmodel.rename.fields.titleread a field'sField<Draft>validation state.- When editing an existing row, call
RenameForm.helpers.fill(row)first soidand existing values populate before submit.