navius

Form

Collect information from your users with native, accessible constraint validation.

Features

  • Renders a native <form>; Enter-to-submit and tab order come for free.
  • Surfaces the browser's built-in ValidityState (valueMissing, typeMismatch, …) with zero validation code.
  • Messages register into aria-describedby while shown and clear when they stop matching.
  • Custom predicates via MatchFn can validate one field against another using FormData.
  • Server errors via ServerInvalid that auto-clear on the next edit.
  • An invalid submit is blocked and focus moves to the first invalid control automatically.

Installation

Install the brain, or copy just this primitive in with the CLI.

Anatomy

Import the parts and assemble them.

API Reference

Wraps the whole form. Renders a native form element and manages the submit, validation and focus flow.

Prop Type Default
OnSubmit EventCallback -
OnClearServerErrors EventCallback -
PreventDefault bool true
ChildContent RenderFragment? -

Groups a label, control and messages under one name. Renders a div.

Prop Type Default
Name string ""
Validity FieldValidity? -
Invalid bool false
ServerInvalid bool false
ChildContent RenderFragment? -
Data attribute Values
[data-valid] Present when the field is valid
[data-invalid] Present when the field is invalid

The field's label. Renders a native label whose for points at the control.

Prop Type Default
ChildContent RenderFragment? -
Data attribute Values
[data-valid] Present when the field is valid
[data-invalid] Present when the field is invalid

The form control. Renders a native input wired with the field id and aria-* by default, or cascades ControlProps to a custom control via the render-fragment child.

Prop Type Default
ChildContent RenderFragment<ControlProps>? -
Data attribute Values
[data-valid] Present when the field is valid
[data-invalid] Present when the field is invalid

A validation message tied to a match. Renders a div only while its match fails, and joins aria-describedby while shown.

Prop Type Default
Match string? -
MatchFn Func<string, FormData?, ValueTask<bool>>? -
Name string? -
ForceMatch bool false
ChildContent RenderFragment? -
Data attribute Values
[data-match] The Match key this message fires on

Render-prop exposing the field's live FieldValidity. Renders nothing itself.

Prop Type Default
ChildContent RenderFragment<FieldValidity>? -
Name string? -

The form's submit button. Renders a native button with type=submit.

Prop Type Default
ChildContent RenderFragment? -
Data attribute Values
[data-navius-form-submit] Present on the submit button

Accessibility

Built on the native <form>, <label> and form controls, so the browser's own constraint-validation and accessibility semantics apply. The label's for points at the control id; active messages are referenced by aria-describedby; and an invalid control carries aria-invalid="true". A failed submit moves focus to the first invalid control.

Key Description
Enter Submits the form. When a field is invalid, submission is blocked and focus moves to the first invalid control.
Tab Moves focus to the next control in the form.