Form
Collect information from your users with native, accessible constraint validation.
<NaviusForm OnSubmit="HandleSubmit">
<NaviusField Name="email">
<NaviusFieldLabel>Email</NaviusFieldLabel>
<NaviusFieldError Match="valueMissing">Please enter your email</NaviusFieldError>
<NaviusFieldError Match="typeMismatch">Enter a valid email address</NaviusFieldError>
<NaviusFieldControl type="email" required placeholder="you@@example.com" />
</NaviusField>
<NaviusFormSubmit>Create account</NaviusFormSubmit>
</NaviusForm>
@code {
private void HandleSubmit() { /* fires only when every field is valid */ }
}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-describedbywhile shown and clear when they stop matching. - Custom predicates via
MatchFncan validate one field against another usingFormData. - Server errors via
ServerInvalidthat 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.
navius add formAnatomy
Import the parts and assemble them.
@using Navius.Primitives.Components.Form
@using Navius.Primitives.Components.Field
<NaviusForm>
<NaviusField Name="...">
<NaviusFieldLabel />
<NaviusFieldControl />
<NaviusFieldError />
<NaviusFieldValidity />
</NaviusField>
<NaviusFormSubmit />
</NaviusForm>API Reference
Root
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? | - |
Field
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 attributes
| Data attribute | Values |
|---|---|
| [data-valid] | Present when the field is valid |
| [data-invalid] | Present when the field is invalid |
Label
The field's label. Renders a native label whose for points at the control.
| Prop | Type | Default |
|---|---|---|
| ChildContent | RenderFragment? | - |
Data attributes
| Data attribute | Values |
|---|---|
| [data-valid] | Present when the field is valid |
| [data-invalid] | Present when the field is invalid |
Control
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 attributes
| Data attribute | Values |
|---|---|
| [data-valid] | Present when the field is valid |
| [data-invalid] | Present when the field is invalid |
Error
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 attributes
| Data attribute | Values |
|---|---|
| [data-match] | The Match key this message fires on |
Validity
Render-prop exposing the field's live FieldValidity. Renders nothing itself.
| Prop | Type | Default |
|---|---|---|
| ChildContent | RenderFragment<FieldValidity>? | - |
| Name | string? | - |
Submit
The form's submit button. Renders a native button with type=submit.
| Prop | Type | Default |
|---|---|---|
| ChildContent | RenderFragment? | - |
Data attributes
| 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.
Keyboard interactions
| 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. |