Form
View as MarkdownA form that submits to an Extbase action with fetch and shows validation errors on its fields.
Features
Permalink to heading "Features"- Submits with
fetchand maps the422validation response of an Extbase action onto the fields - Renders Extbase's hidden
__trustedPropertiesand__identityfields and sends the values under the plugin's argument namespace - Client-side validation with a Standard Schema validator such as Zod, or with a callback
- Errors from native constraints, your schema or callback, async checks of single fields and the server show on the field according to its validation mode
- Form state as
data-state(ready,submitting,invalid,success,error), plusdata-submitting,data-invalid,data-dirtyanddata-touched - Parts for the editable content, state indicators and status text
- Works with every Field-aware primitive and plain HTML inputs
The Forms guide walks through a form from the template to the controller.
<primitives:form.root>
<primitives:form.content>
<primitives:field.root>
<primitives:field.label />
<primitives:field.control asChild="{true}">
<!-- Your form input here -->
</primitives:field.control>
<primitives:field.errorText />
</primitives:field.root>
</primitives:form.content>
<primitives:form.indicator
state="{f:constant(name: 'Jramke\FluidPrimitives\Enum\FormState::Error')}"
>
<primitives:form.errorText />
</primitives:form.indicator>
</primitives:form.root>
Installation
Permalink to heading "Installation"typo3 ui:add form
Please copy the files manually from GitHub into your project: Form
Read more about installing Components and Primitives.
Options that take a function go to Form in your entry file, because a function cannot travel through the server-rendered markup. Mount the form from your entry file as shown in Send the values.
validation- a Standard Schema validator such as Zod, or a synchronous callback that returns errors keyed by field name, see Client-Side ValidationonSubmit- receivesvaluesandapi, and returnstrue,falseor field errors. Send the values withextbase.post(), set the form-level messages withapi.setErrorText()andapi.setSuccessText()render- runs whenever the form state changes, for UI outside the form's own wiring
values uses your field names as dot paths such as person.name and keeps leaf values as string | File. Validation and server errors are keyed by field name too, so a nested field reports under person.name.
Choose when fields validate
Permalink to heading "Choose when fields validate"Set validationMode on the form to give all its fields the same timing, and inputDebounceMs to change how long they wait for a pause in typing before they validate: 100 by default, at least 50. A field that sets its own validationMode keeps it. The modes are described under Validation mode of the field.
<primitives:form.root
validationMode="{f:constant(name: 'Jramke\FluidPrimitives\Enum\ValidationMode::OnBlur')}"
inputDebounceMs="300"
></primitives:form.root>
API Reference
Permalink to heading "API Reference"The following tables cover the available props of the Fluid Primitives.
form.root
Permalink to heading "form.root"Submits and manages the form state. Renders a <form> element.
| Name | Description | Required | Default |
|---|---|---|---|
actionUri | stringThe resolved form action URI to submit to directly. | No | - |
action | stringThe Extbase action name used to build the form action URI. | No | - |
extensionName | stringThe Extbase extension name used to build the form action URI. | No | - |
pluginName | stringThe Extbase plugin name used to build the form action URI. | No | - |
controller | stringThe Extbase controller name used to build the form action URI. | No | - |
arguments | arrayThe Extbase arguments used to build the form action URI. | No | - |
pageUid | intThe target page UID used to build the form action URI. | No | - |
objectName | stringThe object name prefix used for nested form field names. | No | - |
validationMode | Enum\ValidationModeWhen the fields of the form validate, unless a field sets its own `validationMode`. `auto` validates on blur once the value was edited and on every change while an error shows, `onBlur` only on blur, `onChange` on every change, `onSubmit` only on submit. A submit validates every field in every mode. | No | Auto |
inputDebounceMs | integerHow long the fields wait after the last change before they read their value, 100 by default and at least 50. A `validate` that sends a request should not fire on every keystroke. | No | - |
object | mixedThe bound object used for form value mapping. | No | - |
method | stringThe HTTP method used for form submission. | No | 'post' |
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
rootId | stringThe root ID of the component, used for hydration and identification. | No | - |
ids | arrayThe IDs of of the component parts for composition. | No | [] |
autoMount | booleanWhether the client initializes the component on its own. Set it to false to mount it yourself with `mount()` | No | true |
provider | stringThe `id` of the `.provider` this root takes its `rootId` and props from. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
Rendered data attributes
| Attribute | Description |
|---|---|
data-state | The state of the form. One of: "invalid", "ready", "submitting", "success", "error". |
data-submitting | Present while the form is being submitted. |
data-invalid | Present when invalid |
data-dirty | Present when dirty |
data-touched | Present when touched |
form.content
Permalink to heading "form.content"Wraps the editable form UI. It stays visible in ready, invalid, and submitting, and hides in error and success. Renders a <div> element.
| Name | Description | Required | Default |
|---|---|---|---|
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
form.indicator
Permalink to heading "form.indicator"Displays content for an exact form state such as error, success, or submitting. Renders a <div> element.
| Name | Description | Required | Default |
|---|---|---|---|
state | Enum\FormStateThe form state to render. | Yes | - |
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
form.errorText
Permalink to heading "form.errorText"Displays the current form-level error text set through the Form API, or its slotted fallback text. Renders a <span> element.
| Name | Description | Required | Default |
|---|---|---|---|
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
form.successText
Permalink to heading "form.successText"Displays the current form-level success text set through the Form API, or its slotted fallback text. Renders a <span> element.
| Name | Description | Required | Default |
|---|---|---|---|
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
Machine options
Permalink to heading "Machine options"| Name | Description | Default |
|---|---|---|
id | stringThe unique identifier of the form. | - |
validation | FormValidation | undefinedValidates the values of the form on the client: a Standard Schema validator such as Zod, or a synchronous callback that gets the values and returns the errors by field name. It cannot be set from a Fluid template, only by constructing Form in your own entry file. | - |
objectName | string | undefinedThe object name that prefixes the names of nested form fields. | - |
fieldNamePrefix | string | undefinedThe Extbase argument namespace ( tx_events_list) post() prefixes the field names with. Set by the server from the plugin the form submits to. | - |
inputDebounceMs | number | undefinedHow long the fields wait after the last change before they read their value, never less than 50. A validate that sends a request should not fire on every keystroke. | 100 |
validationMode | ValidationMode | undefinedWhen the fields of the form validate, unless a field sets its own validationMode. | "auto" |
onSubmit | (({ values, api, event }: { values: FormValues; api: FormApi; event: JSX.FormEvent<HTMLElement>; }) => Promise<FormSubmitResult> | FormSubmitResult) | undefinedCalled with the values once the form is valid and submitted. Return true for success, false for an error, or field errors to mark the form invalid. Send the values with extbase.post(api.getAction(), values.toObject(), { prefix: api.getFieldNamePrefix(), objectName: api.getObjectName() }): a 422 JSON response of JsonValidationErrorsTrait it throws is mapped to field errors, or to the error text of the form for the errors of the object itself. | - |
render | ((form: Form) => void) | undefinedCalled every time the form state changes. Use it to update UI the form does not wire itself. | - |
Machine JavaScript API
Permalink to heading "Machine JavaScript API"| Name | Type | Description |
|---|---|---|
isSubmitting | boolean | Whether the form is being submitted. |
isDirty | boolean | Whether the value of any field differs from the one it started with. |
isInvalid | boolean | Whether any field is invalid. |
isSuccessful | boolean | Whether the last submit succeeded. |
isError | boolean | Whether the last submit ended in an error. |
getValues | () => FormValues | The current values of the form. |
getErrors | () => FormErrors | The errors of the invalid fields, by field name. |
getDirty | () => FormDirty | The dirty fields, by field name. |
getTouched | () => FormTouched | The touched fields, by field name. |
getErrorText | () => string | null | The text of the error status of the form, null if there is none. |
setErrorText | (text: string | null) => void | Sets the text of the error status. Pass null to remove it. |
getSuccessText | () => string | null | The text of the success status of the form, null if there is none. |
setSuccessText | (text: string | null) => void | Sets the text of the success status. Pass null to remove it. |
clearStatusText | () => void | Removes the error and the success text. |
getAllFields | () => Map<string, FieldHandle> | Every field of the form, by name. |
getField | (name: string) => FieldHandle | undefined | The field with this name, undefined if the form has none. |
getFormEl | () => HTMLFormElement | null | The <form> element. |
getAction | () => string | The action of the form, an empty string if it has none. |
getFieldNamePrefix | () => string | undefined | The Extbase argument namespace (tx_events_list) of the action the form submits to. |
getObjectName | () => string | undefined | The objectName of the form: the name of the object its fields belong to. |
reset | () => void | Resets the form to its initial state. |
syncFields | () => void | Picks up fields that were added or removed after the form was hydrated. |
renameField | (oldName: string, newName: string) => void | Renames a field, keeping its value, touched, dirty and error state. Useful to keep the rows of a FieldArray contiguously indexed after one in the middle was removed. |