Skip to content

Form ​

An enhanced native <form> supporting validation and submission of inner fields according to rules.

Cross-shadow submit entry: oas-form wraps a native <form> inside its shadow root, so buttons in the light DOM (including oas-button) do not have native submit semantics. Call the component's public submit() method at submit time (e.g. this.closest('oas-form').submit()); it delegates to the inner form's requestSubmit(), preserving submit-event semantics and triggering the same validation flow that dispatches oas-submit / oas-validate-fail — do not reach into shadowRoot.querySelector('form') to drive the inner form.

The data source is each field's value attribute (controlled mode). Fields validated by the form are oas-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-value channel) / oas-combobox / oas-picker / oas-transfer / oas-dynamic-input (model-value channel) / oas-checkbox / oas-switch (checked channel) / oas-matrix / oas-rank (JSON string over the value channel) with a name. oas-form has a built-in listener for oas-input / oas-change events bubbling from inner fields and writes back through each field's own value channel (the default value attribute, or model-value for model-value-channel controls; null becomes an empty string) — no per-field wiring needed. Filter input from searchable controls (oas-select in remote mode / oas-combobox / oas-auto-complete) dispatches oas-input with source: '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

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

Submit

Nested name paths: nested values ​

Field name supports dot-path syntax: numeric segments are array indices, other segments are object keys — profile.city assembles into { profile: { city } }; users.0.name and users[0].name are equivalent spellings that assemble into { users: [{ name }] }. On submit, oas-submit's detail.values is the nested structure; rules keys match by the full path string (e.g. "users.0.name"); initial-values writes in through the same nested paths and reset() restores them. Plain names without path syntax assemble exactly as before.

Nested name path submit

Submit

After submitting, values looks like {"name":"Alex","profile":{"city":"Hangzhou"}} — the path segments define the structure.

Dynamic field group form-list ​

oas-form-list maintains an add/remove group of field rows inside the form: put a <template> inside the component as the row template; cloned row fields get their name indexed automatically — either use the {index} placeholder in the template (name="users.{index}.name"), or set name="users" on the component and write bare names in the template (auto-prefixed to users.N.field). Row count has two separate sources: rows only sets the initial row count, while min / max are the remove lower bound / add upper bound (when rows is absent, min doubles as the initial row count for backward compatibility; the remove bound defaults to 0, so minItems violations are reported by validation instead of locking the only row). Each row ends with an action area: duplicate / move up / move down / remove (part values duplicate / move-up / move-down / remove; accessible names come from i18n and can be overridden with duplicate-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 via aria-live; after removal the remaining rows are renumbered with their filled values kept. Fields stay in the light DOM, so validation / submit / oas-values-change all follow the host form; the component dispatches oas-add / oas-remove (detail: { index }) and oas-duplicate / oas-move (detail: { from, to }).

Dynamic field group form-list

Submit

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)

Fill 300 rows

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

Read FormDataform.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 } (validator is a custom validation function; functions are not JSON-serializable, use the rules property channel; validateTrigger overrides the form-level trigger per field). phone / email are built-in format presets: phone validates mainland China mobile numbers (1[3-9]\d{9}), and email uses a practical RFC subset (rejects whitespace, multiple @, missing segments and other clearly invalid input without being overly strict). Like pattern, both skip empty values when the field is not required. Without message, presets use the locale default text (16 locales); the phone / email keys of validate-messages override them at form level. On failure, the field is marked aria-invalid (red-bordered input), an error message is shown in red below the field, and oas-validate-fail is dispatched. The error text is also associated with its control via ElementInternals.ariaDescribedByElements where supported (so even form-associated custom controls in the light DOM can reference an error node inside a shadow root); unsupported browsers keep the role="alert" announcement.

Required & Format Validation ​

Required & format validation

Submit

Length Validation ​

minLength validation

Submit

Built-in phone / email Rules ​

Without message, the phone / email presets use the locale default text (switching with the language pack). A common combination: phone required + format, email optional (empty skipped, but must be valid when filled).

Built-in phone / email rules

Submit

Disabled Fields Skip Validation ​

Disabled fields are not validated

Submit

Submit & Validation-fail Events ​

submit / validate-fail

Submit

Grid Form Layout ​

With layout="grid", the form element becomes a 24-column grid and oas-form-item spans columns via span (default 24 = full row); gap controls grid spacing, label-align positions the label (left / right / top, default top), and label-width sets the label column width for left/right. On validation failure, error messages are collected into the oas-form-item error slot (role="alert").

Two-Column Grid with Validation ​

Two-column grid layout

Submit

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 follows gap (default var(--oas-space-4)), and items wrap when the container is too narrow. Coexists with layout and takes precedence over it; forces label-align to left and label-width to auto. Suited to compact toolbars like login and search.

Inline Login Form ​

Inline login form

Log in

Inline Search Form ​

Inline search form

Search

Controlled syncing and event listeners (wired in one <script> block):

Form-Level Capabilities ​

Whole-Form Disabled ​

The disabled attribute 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's disabled attribute (fields fully recover when released); while the form is disabled, submission skips all validation.

Whole-form disabled

Submit (skips validation while disabled)
Disable the form

Initial Values & Reset ​

initial-values are written into the corresponding fields after mount (input/transfer via value, switch via checked, dynamic-tags/dynamic-input via model-value); reset() restores the initial values and clears validation error states without dispatching any events (consistent with the native form reset baseline). The initialValues property channel is also supported (property takes precedence over attribute).

initial-values and reset

SubmitReset

Custom Validation Function (validator) ​

validator runs after the built-in checks of its rule: return true to pass, a string as the error message, or a Promise for async validation. Functions are not JSON-serializable, so set them through the rules property channel (form.rules = {...} in script).

Custom validation function

Submit

Validation Trigger (validate-trigger) ​

validate-trigger controls when per-field live validation runs: input (default, validate as you type) / blur / change; a rule's validateTrigger overrides 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 uses blur: 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-error set, 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

Submit (scrolls to first error)

Values Change Event (oas-values-change) ​

Dispatched whenever any field value changes, detail: { name, value, values } (values is a snapshot of all current values). Initial-value writes and reset() 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) ​

size distributes the form-level size tier (small / medium / large) to field controls inside the form. Priority: a field's own explicit size > the form-level size > config-provider global injection > the default tier (when both form-level and global injection are present, the form-level wins). colon renders a colon after the label of every oas-form-item in the form. Click the buttons below to watch fields resize and the colon toggle live.

Form-level size + colon

Checkbox follows the form-level sizeOwn size=medium wins (ignores form level)Switch to smallSwitch to largeToggle colon

Help Text & Status Icon (help / status-icon) ​

help renders persistent helper text below the control, independent of validation errors. With status-icon on a form-item, a danger status icon appears before the error text when validation fails (hidden again once the error clears).

help + status-icon

Submit

Custom Validation Messages (validate-messages) ​

validateMessages (property channel, recommended) or validate-messages (JSON attribute) overrides locale-default messages per rule type. Templates support ${min} / ${max} / ${value} placeholders; an explicit rule.message has the highest priority.

validate-messages

Submit

API ​

oas-form ​

Attributes ​

AttributeDescriptionTypeDefault
colonRenders a colon after every form-item label in the form (CSS ::after, no DOM text)——
disabledWhole-form disable: fields are disabled via their form-associated channel (no write-back of the disabled attribute); submission skips validation while disabledboolean—
gapSpacing (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 inlinestring0
initial-valuesInitial values JSON (the initialValues property takes precedence): written into fields after mount; reset() restores themRecord<string, unknown> | string—
inlineInline 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 autoboolean—
label-alignLabel alignment: left / right / top (default top in grid mode; forced to left in inline mode)stringtop
label-widthLabel column width when label-align is left/right (ignored in inline mode, auto)——
layoutLayout mode: vertical (default, stacked) / grid (24-column grid); non-enum values fall back to vertical; inline attribute takes precedence when presentstringvertical
rulesValidation 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-errorOn validation failure, focus the first invalid field and smooth-scroll it into view (instant jump under prefers-reduced-motion)boolean—
sizeForm-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-messagesOverrides locale-default validation messages per rule type (property/JSON channels, ${min}/${max}/${value} placeholders, default fallback)ValidateMessages | string—
validate-triggerWhen per-field live validation fires: input (default, validate as you type) / blur / change; a rule's validateTrigger overrides per field; submission always validates everythingstringinput

Methods ​

MethodSignatureDescription
reapplyInitialValuesreapplyInitialValues(): voidWrites initial-values for fields that appear only after the initial-value baseline is established (dynamic field containers such as oas-form-list)
getValuesgetValues(): 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()
setValuessetValues(patch: Record<string, unknown>): voidControlled 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
validatevalidate(): Promise<boolean>Public validation entry: validates all fields and syncs error state (aria-invalid + error message), returning whether everything passed
submitsubmit(): voidPublic submit entry: delegates to requestSubmit() on the internal form
resetreset(): voidReset to the initial values (matching form-associated reset baseline semantics: restores initial values, clears validation errors, dispatches no events)
setErrorssetErrors(list: { path: string; message: string; severity?: 'error' | 'warning' }[]): voidInject 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
clearErrorsclearErrors(paths?: string[]): voidClear injected errors (omit paths to clear all); affected fields fall back to rule errors

Events ​

EventDescription
oas-submitValidation passed, detail: { values }
oas-validate-failValidation failed, detail: { errors, values }
oas-values-changeDispatched when any field value changes, detail: { name, value, values } (values is a whole-form snapshot; initial-value writes and reset() are silent)

Slots ​

NameDescription
defaultForm content (oas-form-item and friends)

oas-form-item ​

Attributes ​

AttributeDescriptionTypeDefault
helpPersistent helper text below the control, independent of validation errorsstring—
labelLabel text (no label row when omitted)string—
nameField name (validation association)——
requiredRequired asterisk (visual only; validation is still driven by form rules)boolean—
spanColumns spanned in the 24-column grid (only when form layout="grid"; non-integer in 1-24 → 24)string24
status-iconShows a danger status icon before the error text while the error is presentboolean—

Methods ​

MethodSignatureDescription
setErrorsetError(message: string | null, severity?: 'error' | 'warning'): voidValidation error/warning sink (called by oas-form): error uses role=alert and takes exclusive precedence; warning uses role=status (no aria-invalid, non-blocking)
refreshLayoutrefreshLayout(): voidCalled by the parent oas-form when layout attributes change, to immediately refresh grid/label layout awareness

Slots ​

NameDescription
defaultField control

CSS Variables ​

CSS VariableDefault
--oas-form-label-width96px

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").

Released under the MIT OR Apache-2.0 License.