Form
An enhanced native <form> supporting validation and submission of inner fields according to rules.
Cross-shadow submit entry:
oas-formwraps a native<form>inside its shadow root, so buttons in the light DOM (includingoas-button) do not have native submit semantics. Call the component's publicsubmit()method at submit time (e.g.this.closest('oas-form').submit()); it delegates to the inner form'srequestSubmit(), preserving submit-event semantics and triggering the same validation flow that dispatchesoas-submit/oas-validate-fail— do not reach intoshadowRoot.querySelector('form')to drive the inner form.
The data source is each field's
valueattribute (controlled mode). Fields validated by the form areoas-input/oas-textarea/oas-select/oas-auto-complete/oas-cascader/oas-tree-select/oas-input-number/oas-radio/oas-radio-group/oas-checkbox-group/oas-date-picker/oas-time-picker/oas-slider/oas-rate/oas-pin-input/oas-dynamic-tags(model-valuechannel) /oas-combobox/oas-picker/oas-transfer/oas-dynamic-input(model-valuechannel) /oas-checkbox/oas-switch(checkedchannel) /oas-matrix/oas-rank(JSON string over thevaluechannel) with aname.oas-formhas a built-in listener foroas-input/oas-changeevents bubbling from inner fields and writes back through each field's own value channel (the defaultvalueattribute, ormodel-valueformodel-value-channel controls; null becomes an empty string) — no per-field wiring needed. Filter input from searchable controls (oas-selectin remote mode /oas-combobox/oas-auto-complete) dispatchesoas-inputwithsource: 'search'; the form does not treat it as a field value.
Feature Demo
The feature demo area only demonstrates field collection and submission, without validation rules.
Basic Usage
Collect & submit
Without rules, submission performs no validation and dispatches oas-submit directly, with detail.values carrying the collected results of all fields with a name.
Mixed Controls
Mixed controls
Nested name paths: nested values
Field
namesupports dot-path syntax: numeric segments are array indices, other segments are object keys —profile.cityassembles into{ profile: { city } };users.0.nameandusers[0].nameare equivalent spellings that assemble into{ users: [{ name }] }. On submit,oas-submit'sdetail.valuesis the nested structure;ruleskeys match by the full path string (e.g."users.0.name");initial-valueswrites in through the same nested paths andreset()restores them. Plain names without path syntax assemble exactly as before.
Nested name path submit
After submitting, values looks like {"name":"Alex","profile":{"city":"Hangzhou"}} — the path segments define the structure.
Dynamic field group form-list
oas-form-listmaintains an add/remove group of field rows inside the form: put a<template>inside the component as the row template; cloned row fields get theirnameindexed automatically — either use the{index}placeholder in the template (name="users.{index}.name"), or setname="users"on the component and write bare names in the template (auto-prefixed tousers.N.field). Row count has two separate sources:rowsonly sets the initial row count, whilemin/maxare the remove lower bound / add upper bound (whenrowsis absent,mindoubles as the initial row count for backward compatibility; the remove bound defaults to 0, sominItemsviolations are reported by validation instead of locking the only row). Each row ends with an action area: duplicate / move up / move down / remove (partvaluesduplicate/move-up/move-down/remove; accessible names come from i18n and can be overridden withduplicate-text/move-up-text/move-down-text/remove-text). Duplicate deep-copies the row value into the next row; move up / down swap values (DOM order stays). After an operation focus returns by row position and the action is announced viaaria-live; after removal the remaining rows are renumbered with their filled values kept. Fields stay in the light DOM, so validation / submit /oas-values-changeall follow the host form; the component dispatchesoas-add/oas-remove(detail: { index }) andoas-duplicate/oas-move(detail: { from, to }).
Dynamic field group form-list
After submitting, values looks like {"members":[{"name":"Alex","role":"self"},{"name":"Sam","role":"relative"}]}; in plain HTML write the row template as a literal <template>, while in md/Vue compiled contexts prefer injecting an HTMLTemplateElement built in JS (template content pass-through is unreliable under template compilation).
form-list virtual scrolling (large datasets)
For thousand-row arrays add virtual: only the visible window plus overscan renders (5 rows per side by default, adjustable via overscan), spacers keep the total height stable (real scrollbar), and row heights are estimated on first render then calibrated from measurements (estimated-row-height seeds the initial estimate; the default estimate is overwritten by measured self-calibration). Fields all stay in the light DOM — validation / submit / name renumbering / row operations / controlled setRowCount semantics are unchanged; virtualization happens only at the shadow-frame layer: non-windowed rows render nothing and cost no layout, while their values are still collected by the host form. When present, the viewport-height attribute is synced into the container's inline max-height (box height and window calculation share one source, so tail rows stay reachable when scrolled to the bottom; removing the attribute falls back to the CSS default of 320px, with the CSS --oas-form-list-viewport-height override). Row gaps (8px in the default theme) count into the height pitch, so the virtual content height matches full rendering; in virtual mode "add row" scrolls the new row into view and focuses its first field. Screen-reader line-by-line traversal only covers the current window rows (an inherent virtualization trade-off; the row total is carried by the rowCount property and row-operation aria-live announcements).
form-list virtual scrolling (click to fill 300 rows, scroll to watch windowing)
Native Form Integration (form-associated)
Fifteen common form components are form-associated custom elements (formAssociated: true): they work directly inside a native <form> — <label for> association works (clicking the label focuses/activates the control, screen readers announce the label text), values are collected via standard FormData (submitted only when name is set), form.reset() restores initial values, fieldset[disabled] disables them, and required joins the native validation chain (checkValidity() / :invalid pseudo-class).
native form + label for + FormData + reset
Supported (15 components): input / textarea / input-number / checkbox / radio / radio-group / switch / select / combobox / auto-complete / tree-select / mentions / date-picker / time-picker / upload. Value semantics follow the native ones: multiple mode (select / tree-select / date-picker multiple) submits same-name entries; date / time range mode submits name-start / name-end; upload submits File; unchecked checkables submit nothing. Works in parallel with oas-form's collectFields mechanism.
Validation
The validation area demonstrates rules-declared validation rules and failure feedback.
Validation rules:
{ required, message, minLength, maxLength, pattern, phone, email, validator, validateTrigger }(validatoris a custom validation function; functions are not JSON-serializable, use therulesproperty channel;validateTriggeroverrides the form-level trigger per field).phone/phonevalidates mainland China mobile numbers (1[3-9]\d{9}), and@, missing segments and other clearly invalid input without being overly strict). Likepattern, both skip empty values when the field is notrequired. Withoutmessage, presets use the locale default text (16 locales); thephone/validate-messagesoverride them at form level. On failure, the field is markedaria-invalid(red-bordered input), an error message is shown in red below the field, andoas-validate-failis dispatched. The error text is also associated with its control viaElementInternals.ariaDescribedByElementswhere supported (so even form-associated custom controls in the light DOM can reference an error node inside a shadow root); unsupported browsers keep therole="alert"announcement.
Required & Format Validation
Required & format validation
Length Validation
minLength validation
Built-in phone / email Rules
Without
message, thephone/
Built-in phone / email rules
Disabled Fields Skip Validation
Disabled fields are not validated
Submit & Validation-fail Events
submit / validate-fail
Grid Form Layout
With
layout="grid", the form element becomes a 24-column grid andoas-form-itemspans columns viaspan(default 24 = full row);gapcontrols grid spacing,label-alignpositions the label (left/right/top, defaulttop), andlabel-widthsets the label column width forleft/right. On validation failure, error messages are collected into theoas-form-itemerror slot (role="alert").
Two-Column Grid with Validation
Two-column grid layout
label-align & label-width
Switch label-align
Inline Form Layout
With
inline, items are laid out horizontally: the label sits on the left of the control (auto-width), controls size to their content, item spacing followsgap(defaultvar(--oas-space-4)), and items wrap when the container is too narrow. Coexists withlayoutand takes precedence over it; forceslabel-aligntoleftandlabel-widthto auto. Suited to compact toolbars like login and search.
Inline Login Form
Inline login form
Inline Search Form
Inline search form
Controlled syncing and event listeners (wired in one <script> block):
Form-Level Capabilities
Whole-Form Disabled
The
disabledattribute aligns with the config-provider global-disable semantics: all fields are disabled through their own form-associated disable channel (inner controls disabled, interaction blocked), without writing back the field'sdisabledattribute (fields fully recover when released); while the form is disabled, submission skips all validation.
Whole-form disabled
Initial Values & Reset
initial-valuesare written into the corresponding fields after mount (input/transfer viavalue, switch viachecked, dynamic-tags/dynamic-input viamodel-value);reset()restores the initial values and clears validation error states without dispatching any events (consistent with the native form reset baseline). TheinitialValuesproperty channel is also supported (property takes precedence over attribute).
initial-values and reset
Custom Validation Function (validator)
validatorruns after the built-in checks of its rule: returntrueto pass, a string as the error message, or a Promise for async validation. Functions are not JSON-serializable, so set them through therulesproperty channel (form.rules = {...}in script).
Custom validation function
Validation Trigger (validate-trigger)
validate-triggercontrols when per-field live validation runs:input(default, validate as you type) /blur/change; a rule'svalidateTriggeroverrides the form level. Submission always validates everything. The default live-on-input validation settles errors before the user clicks submit, so an error appearing/disappearing at click time can never shift a control out from under the pointer and swallow the click. Once a field has an error, every subsequent keystroke re-validates it live (clearing as soon as it is corrected). The example below usesblur: type an invalid phone number, then click elsewhere (blur) to see the red error text; fix it and blur again to clear it.
Validate on blur
Scroll to First Error (scroll-to-first-error)
With
scroll-to-first-errorset, a failed submit focuses the first invalid field and smooth-scrolls it into view (degrades to an instant jump when the system prefers reduced motion). The long form below scrolls — the remark field sits at the very bottom; click submit and watch the container scroll down.
Scroll to first error
Values Change Event (oas-values-change)
Dispatched whenever any field value changes,
detail: { name, value, values }(valuesis a snapshot of all current values). Initial-value writes andreset()are silent channels and do not dispatch.
oas-values-change
Script wiring (controlled sync + event feedback for each demo):
Form-Level Size & Label Colon (size / colon)
sizedistributes the form-level size tier (small/medium/large) to field controls inside the form. Priority: a field's own explicitsize> the form-levelsize> config-provider global injection > the default tier (when both form-level and global injection are present, the form-level wins).colonrenders a colon after the label of everyoas-form-itemin the form. Click the buttons below to watch fields resize and the colon toggle live.
Form-level size + colon
Help Text & Status Icon (help / status-icon)
helprenders persistent helper text below the control, independent of validation errors. Withstatus-iconon a form-item, a danger status icon appears before the error text when validation fails (hidden again once the error clears).
help + status-icon
Custom Validation Messages (validate-messages)
validateMessages(property channel, recommended) orvalidate-messages(JSON attribute) overrides locale-default messages per rule type. Templates support${min}/${max}/${value}placeholders; an explicitrule.messagehas the highest priority.
validate-messages
API
oas-form
Attributes
| Attribute | Description | Type | Default |
|---|---|---|---|
colon | Renders a colon after every form-item label in the form (CSS ::after, no DOM text) | — | — |
disabled | Whole-form disable: fields are disabled via their form-associated channel (no write-back of the disabled attribute); submission skips validation while disabled | boolean | — |
gap | Spacing (grid gap in grid mode; item spacing in inline mode), token value e.g. var(--oas-space-4); 0 by default in grid, var(--oas-space-4) by default in inline | string | 0 |
initial-values | Initial values JSON (the initialValues property takes precedence): written into fields after mount; reset() restores them | Record<string, unknown> | string | — |
inline | Inline layout: items laid out horizontally (label on the left of the control, controls auto-width, wraps to new lines); item spacing follows gap (default var(--oas-space-4)); coexists with layout and takes precedence over it; forces label-align to left and label-width to auto | boolean | — |
label-align | Label alignment: left / right / top (default top in grid mode; forced to left in inline mode) | string | top |
label-width | Label column width when label-align is left/right (ignored in inline mode, auto) | — | — |
layout | Layout mode: vertical (default, stacked) / grid (24-column grid); non-enum values fall back to vertical; inline attribute takes precedence when present | string | vertical |
rules | Validation rules JSON: { fieldName: [{ required, message, minLength, maxLength, pattern, phone, email }] } (phone/email are built-in format presets, empty values skipped, locale-default messages) | Rules | string | — |
scroll-to-first-error | On validation failure, focus the first invalid field and smooth-scroll it into view (instant jump under prefers-reduced-motion) | boolean | — |
size | Form-level size tier distributed to all fields via the data-form-size channel (a field's own explicit size wins; form-associated fields follow dynamically). Boundary: data-form-size is a static/inspection channel — writing it directly on a field host does not react immediately; dynamic following only happens through the oas-form size distribution path | — | — |
validate-messages | Overrides locale-default validation messages per rule type (property/JSON channels, ${min}/${max}/${value} placeholders, default fallback) | ValidateMessages | string | — |
validate-trigger | When per-field live validation fires: input (default, validate as you type) / blur / change; a rule's validateTrigger overrides per field; submission always validates everything | string | input |
Methods
| Method | Signature | Description |
|---|---|---|
reapplyInitialValues | reapplyInitialValues(): void | Writes initial-values for fields that appear only after the initial-value baseline is established (dynamic field containers such as oas-form-list) |
getValues | getValues(): Record<string, unknown> | Public value getter: a snapshot of the current values for the whole form (nested structure), with the same shape as detail.values dispatched by submit() |
setValues | setValues(patch: Record<string, unknown>): void | Controlled write channel: writes field values by nested path through the control writer registry; a path that exactly matches a rendered field name is written as a whole value (array-valued fields such as oas-checkbox-group are not split into index paths) while container keys still descend; only rendered fields are written, no events are emitted and the reset baseline is unchanged |
validate | validate(): Promise<boolean> | Public validation entry: validates all fields and syncs error state (aria-invalid + error message), returning whether everything passed |
submit | submit(): void | Public submit entry: delegates to requestSubmit() on the internal form |
reset | reset(): void | Reset to the initial values (matching form-associated reset baseline semantics: restores initial values, clears validation errors, dispatches no events) |
setErrors | setErrors(list: { path: string; message: string; severity?: 'error' | 'warning' }[]): void | Inject errors (server validation / upstream containers): paths are normalized by parseNamePath; injected errors take precedence over rule errors until the field value changes again; array-path errors use the same channel and appear in the oas-validate-fail payload |
clearErrors | clearErrors(paths?: string[]): void | Clear injected errors (omit paths to clear all); affected fields fall back to rule errors |
Events
| Event | Description |
|---|---|
oas-submit | Validation passed, detail: { values } |
oas-validate-fail | Validation failed, detail: { errors, values } |
oas-values-change | Dispatched when any field value changes, detail: { name, value, values } (values is a whole-form snapshot; initial-value writes and reset() are silent) |
Slots
| Name | Description |
|---|---|
| default | Form content (oas-form-item and friends) |
oas-form-item
Attributes
| Attribute | Description | Type | Default |
|---|---|---|---|
help | Persistent helper text below the control, independent of validation errors | string | — |
label | Label text (no label row when omitted) | string | — |
name | Field name (validation association) | — | — |
required | Required asterisk (visual only; validation is still driven by form rules) | boolean | — |
span | Columns spanned in the 24-column grid (only when form layout="grid"; non-integer in 1-24 → 24) | string | 24 |
status-icon | Shows a danger status icon before the error text while the error is present | boolean | — |
Methods
| Method | Signature | Description |
|---|---|---|
setError | setError(message: string | null, severity?: 'error' | 'warning'): void | Validation error/warning sink (called by oas-form): error uses role=alert and takes exclusive precedence; warning uses role=status (no aria-invalid, non-blocking) |
refreshLayout | refreshLayout(): void | Called by the parent oas-form when layout attributes change, to immediately refresh grid/label layout awareness |
Slots
| Name | Description |
|---|---|
| default | Field control |
CSS Variables
| CSS Variable | Default |
|---|---|
--oas-form-label-width | 96px |
On validation failure, failed fields are marked aria-invalid; error messages can be retrieved via form.getErrors(). For fields wrapped in oas-form-item, the error text is collected into the form-item's error slot (role="alert").