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>;

Submit pipeline#

  1. Validate all controls in parallel (marks them touched). On errors: focus the first, announce, emit invalid.
  2. Bot guard (minimum fill time, trusted interaction count). On failure: emit blocked.
  3. Client rate limit. On failure: emit blocked with the retry time.
  4. Emit submitting, then call onSubmit (the bound button shows a spinner).
  5. Apply fieldErrors, wipe secrets per wipeSecrets, reset the limiter on success, emit success or failure.

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.