FormRenderer
FormRenderer builds a form from a plain JSON schema: every control of the library inside a FormField, validation with built-in and custom validators, declarative conditions, sections, columns, steps and repeatable groups, on top of Reactive Forms.
Import #
import { FormRenderer, FormRendererField, FormSchema } from '@exhaustive-ui/core/formrenderer';Basic #
FormRenderer builds a form from a schema, a plain JSON object you can store, version and send from a server. Every field is a control of the library inside a FormField, with its label, hint and errors. The value is bound with [(value)], and onSubmit only fires when the form is valid: otherwise every field is marked as touched, an error summary lists the problems and the focus moves to the first invalid field.
Schema Reference #
A schema has a version, always 1 for now, an optional title and description, and its fields in one of three layouts: fields, sections or steps. A field has a unique key, the name of its value, and a type. Where it costs nothing the names follow JSON Schema: default, minimum, maximum, minLength, maxLength, pattern, minItems, maxItems and readOnly.
| Type | Control | Value |
|---|---|---|
| text, email, password | InputText, Password | string |
| number | InputNumber | number |
| textarea | Textarea | string |
| select, multiselect | Select, MultiSelect | value, array of values |
| radio | RadioButton group | value |
| checkbox, toggle | Checkbox, ToggleSwitch | boolean |
| date, daterange, time | DatePicker, with presets for ranges | Date, [Date, Date], Date |
| color, rating, slider | ColorPicker, Rating, Slider | string, number, number |
| otp, phone, file | InputOtp, PhoneInput, FileInput | string, string, File or File[] |
| group | A fieldset of sub-fields | object |
| array | A repeatable group of sub-fields | array of objects |
The options of a choice are strings, numbers or { label, value, disabled } objects, or an { loader } source for options that come from an async loader. Settings specific to a control, such as rows, stars, showTime, presets, mode and currency, go in props. An unknown type falls back to a text input, so a schema written for a newer version still renders.
Validation #
required, minimum, maximum, minLength, maxLength, pattern, minItems and maxItems become validators, and the email type checks the address. equals names another field whose value this one must repeat, such as a password confirmation. Custom validators are referenced by name in validators, with optional params and message, and registered with the validators input or once for the application with provideFormRendererValidators. A validator returns null when the value is valid, or a message.
The messages come from the locale texts of FormField, so a translation applies to every form; messages replaces them for one field, with {param} placeholders.
import { provideFormRendererValidators } from '@exhaustive-ui/core/formrenderer';
bootstrapApplication(App, {
providers: [
provideFormRendererValidators({
even: (value) => (value == null || value % 2 === 0 ? null : 'Enter an even number.'),
after: (value, params, { formValue }) => (!value || !formValue[params.field] || value > formValue[params.field] ? null : 'Pick a later date.')
})
]
});Conditions #
visibleWhen, disabledWhen and requiredWhen are declarative conditions in the format of FilterBuilder: groups joined by AND or OR, holding rules with a field, an operator and a value. They are plain JSON like the rest of the schema, and FormBuilder edits them with a FilterBuilder. A hidden field is removed from the value and from the validation, a disabled one keeps its value out of the validation. Inside a group or a repeatable group, a condition can test the fields of the same item by their key.
Layout and Steps #
sections group the fields in fieldsets with a legend and a description, and steps split the sections over a Stepper: Next validates the current step before moving on, and submitting from the last step validates them all and opens the first one with an error. columns sets the grid of the form or of a section and colSpan the width of a field; the grid becomes a single column when its container is narrow.
A group nests its sub-fields under its key, and an array repeats them, with buttons to add and remove items, minItems and maxItems, and the props.itemLabel of an item, such as Guest {0}.
Custom Fields #
A pFormRendererField template renders the fields of a type of your own, or replaces a built-in type. It receives the field as its implicit value and the control, a FormControl, along with the id to give to the control so the label and the error summary point to it, and its invalid, disabled, readonly and required states. The template is rendered inside the FormField of the field, which keeps showing its label, hint and errors. Options from an async source come from loaders, by the name in options.loader; with dependsOn they reload when another field changes.
Forms Integration #
FormRenderer is built on Reactive Forms: each field is a FormControl, a group a FormGroup and a repeatable group a FormArray, so conditions can enable and disable controls and items can be added at runtime. Pass your own FormGroup with form to read its state, patch it or combine it with other controls; FormRenderer adds the controls of the schema to it. It is also available as the formGroup signal. labelVariant sets the label variant of every FormField, and disabled and readonly apply to the whole form.
Status: INVALID
Accessibility #
Screen Reader
FormRenderer renders a native form named by ariaLabel or by its title, whose level is set by headingLevel. Every field gets the label, hint and errors of FormField, linked with aria-describedby, and aria-required and aria-invalid on its control. Sections, groups, radio groups and the items of a repeatable group are fieldsets with a legend.
When a submit fails, an error summary with the alert role lists the errors as links to their fields, and the focus moves to the first invalid field. Steps are a Stepper whose current step is announced; moving to another step, adding or removing an item and resetting the form are announced by a polite live region. Every text is translated with the formRenderer key of the locale texts or replaced with the labels input.
Keyboard Support
The fields are the standard controls of the library in reading order, each with its own keyboard support.
| Key | Function |
|---|---|
| tab / shift + tab | Moves the focus to the next or the previous field or button. |
| enter | In a text field, submits the form, or validates the step and moves to the next one. |
| enter / space | On a link of the error summary, moves the focus to its field; on a button, adds or removes an item, moves between steps, resets or submits. |