Fragiola

API: components

Form

A native form that validates its fields on submission, focuses the first invalid one, shows server errors and sets its fields' validation mode.

A native <form> that runs the Field layer's validation itself: its submission validates every field, shows their errors, focuses the first invalid field and is blocked while one is invalid. It shows server errors on the fields they name, and gives its fields a default validationMode. The APG has no form pattern; it follows Base UI's Form (the behaviour rule). Form is one component, not a namespace.

Controls the browser cannot validate (a Slider, a CheckboxGroup) block a Form's submission through their field's validate, which a plain <form> cannot do.

Create an account
Gallery

Behaviour

  • novalidate: the Form validates, not the browser, and shows no browser bubble.
  • A submission (a submit button, Enter in a text field, requestSubmit()): every Field inside validates as on a submission attempt, and shows its errors; an onSubmit field validates on every change from then on. While a field is invalid (its validity, invalid, or server errors for it), the submission is blocked before any listener of yours runs, and focus moves to the first invalid field in document order (a text input's text is selected). Otherwise your submit listener runs: it may still preventDefault and send the FormData with fetch.
  • Server errors: errors is { [fieldName]: string | string[] }, keyed by the Fields' name. The Form keeps its own copy; a new object replaces it. A field with errors is invalid and its Error with no match shows them. When errors changes after a submission went on (the server answered), focus moves to the first invalid field.
  • Clearing: the first change of a field's value hides its server errors at once, and onClearErrors receives the errors left (that field's key removed), whether or not you pass the new object back.
  • validationMode: the default of every Field inside it; a Field's own wins.
  • A form reset clears neither the Form's errors nor the fields' states, as in Base UI.

Anatomy

partelementdata-primitives-partrole
Formformform-rootnone of its own; a form with an accessible name (aria-label)

Usage

import { Field } from "@fragiola/primitives-react/field";
import { Form, type FormErrors } from "@fragiola/primitives-react/form";

const [errors, setErrors] = useState<FormErrors>({});

<Form
    aria-label="Create account"
    validationMode="onBlur"
    errors={errors}
    onClearErrors={setErrors}
    onSubmit={async (event) => {
        event.preventDefault();
        const response = await fetch("/api/accounts", {
            method: "POST",
            body: new FormData(event.currentTarget),
        });
        if (!response.ok) setErrors((await response.json()) as FormErrors);
    }}
>
    <Field.Root name="username">
        <Field.Label>Username</Field.Label>
        <Field.Control required />
        <Field.Error />
    </Field.Root>
    <button type="submit">Create account</button>
</Form>;

Props

It also takes what the primitive contract gives: render (Vue: as and the render slot), a class and a style, a ref, and any attribute, which it forwards (action, method, ...). Vue binds them as :errors, validation-mode and @clear-errors; Angular as [errors], validationMode and (clearErrors).

Form

proptypedefaultdescription
errorsFormErrors ({ [name]: string | string[] }){}server errors, keyed by the Fields' name
onClearErrors(errors: FormErrors) => voida field's value changed while the Form had errors for it: the errors left
validationMode"onSubmit" | "onBlur" | "onChange""onSubmit"the default of every Field inside it
onSubmit (react)(event: FormEvent<HTMLFormElement>) => voidthe native submit event, only for a submission that goes on (Vue: @submit; Angular: (submit), both native)

State and part attributes

partattributes
Formnovalidate

The fields inside carry their own state (Field); a field with server errors carries data-invalid and aria-invalid="true".

Keyboard

Nothing of its own: Enter in a text field submits, natively.

Exports

entryexports
@fragiola/primitives-react/formFieldValidationMode, Form, FormErrors, FormProps, FormState, RenderProp
@fragiola/primitives-vue/formFieldValidationMode, Form, FormEmits, FormErrors, FormProps, FormState, RenderSlotProps
@fragiola/primitives-angular/formFieldValidationMode, Form, FormErrors