Form / foldkit-form

Nested input

A key mapped with Relation.nested holds child rows, each managed as a Model of the nested form:

ts
const NewComment = Entity.input(Blog.Comment, Schema.Struct({ body: Schema.String }))
const CreatePost = Entity.input(
  Blog.Post,
  Schema.Struct({ title: Schema.String, comments: Schema.Array(NewComment.schema) }),
  { comments: Relation.nested(Blog.Post.relations.comments, NewComment) },
)

// The form that edits a comment alone is the form a post's form nests.
const CommentForm = Form.make('Comment', NewComment, { inputs: { body: Input.multiline() } })
const PostForm = Form.make('PostForm', CreatePost, { nested: { comments: CommentForm } })

PostForm.row('comments', 'r0').Changed({ key: 'body', value: 'First' }) // a Message of PostForm
PostForm.nested.comments // CommentForm

PostForm.Message.RowAdded({ key: 'comments' })
PostForm.Message.RowRemoved({ key: 'comments', row: 'r0' })
PostForm.rows(PostForm.initial, 'comments') // [{ id, model }], each a Model of the nested form
The key isRows
Required one relationOne row from the start; stays present
Optional one relationZero or one row; empty submits null/undefined
many relationZero or more rows
  • Pass nested: { comments: CommentForm } with a form built from the same target input (omitting it generates a default form inheriting messages and debounce).
  • PostForm.row('comments', id) constructs typed Messages (Changed, Blurred, Searched, RowAdded, send(msg)) targeting model.rows; stale row IDs are ignored and row IDs are unique across the form's lifetime.
  • Nested controls have kind Nested (Input.Nested.is(control)), holding { cardinality, optional, form }.
  • Submitted validates every row, waits on row async checks, surfaces list-level errors (such as Schema.isMaxLength(5)) in model.errors, and fill populates nested rows recursively.