Chapter 6
The Form controller
const form = new Form(ui, {
onSubmit, // required
autovalidateMode: 'onUserInteraction', // 'disabled' | 'onUserInteraction' | 'always'
rateLimit: { maxAttempts: 5, windowMs: 60_000, lockoutMs: 15_000, maxLockoutMs: 900_000 }, // or false
botGuard: { minFillMs: 2000, minInteractions: 3 }, // or false
idleTimeoutMs: 5 * 60_000, // wipe secrets after inactivity; 0 = off
wipeSecrets: 'always', // 'always' | 'success' | 'never'
});
onSubmit contract#
type SubmitResult = { ok: boolean; message?: string; fieldErrors?: Record<string, string> };
onSubmit: (values: FormValues, form: Form) => SubmitResult | void | Promise<SubmitResult | void>;
valuesis an object keyed by controlname(buttons excluded). Secrets arrive asSecret.- Return nothing to mean success. If
onSubmitthrows, the result is a generic failure, and error details are never shown to the user. fieldErrorskeys must match control names. Each message appears on its field and the first one gets focus.
Submit pipeline#
- Validate all controls in parallel (marks them touched). On errors: focus the first, announce, emit
invalid. - Bot guard (minimum fill time, trusted interaction count). On failure: emit
blocked. - Client rate limit. On failure: emit
blockedwith the retry time. - Emit
submitting, then callonSubmit(the bound button shows a spinner). - Apply
fieldErrors, wipe secrets perwipeSecrets, reset the limiter on success, emitsuccessorfailure.
Members and events#
| Member | Purpose |
|---|---|
add(...controls) |
Register controls (throws on duplicate names). Chainable. |
bindSubmit(button) |
Make a button submit the form. |
get<T>(name) |
Look up a control. |
values() |
Current values. |
validate() |
Validate everything; returns { name: error }. |
submit() |
Run the pipeline programmatically. |
wipeSecrets() |
Zero all secret fields (emits change on them). |
reset() |
Reset all controls and wipe secrets. |
isSubmitting |
Whether a submit is in flight. |
destroy() |
Remove listeners and wipe. |
Events: submitting, success(result), failure(result), invalid(errors), blocked(reason), idle.