FormField
FormField gives any control a label, a hint and error messages, wired for screen readers without an id to write. It reads the errors of Reactive forms, template-driven forms and Signal Forms, turns their keys into localized messages, marks required fields and counts characters, with the label above, beside or inside the field.
Import #
import { FormFieldModule, provideFormFieldMessages } from '@exhaustive-ui/core/formfield';FormFieldModule exports the component and its pFormFieldHint, pFormFieldError and pFormFieldControl directives. The component class is UIFormField, so that it never clashes with the FormField directive of Angular Signal Forms, the one behind [formField]; the element stays p-form-field.
Basic #
FormField wraps a control and gives it a label and a hint. There is no id to write: the control gets one, the label points to it and the hint describes it, so screen readers read "Email, edit text, We never share it." Clicking the label focuses the control.
Hint #
The hint input is a text. For markup, such as a link or emphasis, use a #hint template, or put the pFormFieldHint directive on elements of your own: each one gets an id and joins the aria-describedby of the control, after the hint.
Errors #
Reactive Forms #
With reactive forms, FormField reads the control bound by formControlName or [formControl]: the required validator adds the asterisk, and the first error is shown once the control is touched or the form is submitted, with a message for its key and its parameters, such as "Enter at least 3 characters." The control gets aria-invalid and is described by the error, which a polite live region announces.
Template Driven Forms #
With template-driven forms, FormField reads the ngModel of the control and its validation attributes: required adds the asterisk, minlength, email and pattern give their messages.
Signal Forms #
With Angular Signal Forms, FormField reads the state of the field bound by [formField]: its required state, its errors and the message of each validator, which comes before the default messages. The minLength and maxLength errors take the same messages as the minlength and maxlength of Reactive forms. submit() marks the fields as touched, which shows their errors.
When Errors Show #
showErrors sets when the errors of the control appear: touched, the default, once the user leaves the field; dirty, as soon as the value changes; submitted, only after a submit; always, at once. All of them show the errors after a submit. showAllErrors lists every error instead of the first one.
Custom Messages #
A message is found by the key of the error, in this order: the errorMessages of the field, the message given by the validator (the message of a Signal Forms validator, or a text returned as the value of a custom validator error), the messages provided with provideFormFieldMessages, the formErrors of the translation, then English defaults. A message is a text with placeholders for the parameters of the error and {label}, or a function; give a factory to provideFormFieldMessages to read a translation service.
For markup, an ng-template with pFormFieldError and a key renders that error, with its parameters as context. The error input shows an error of your own, such as an answer of the server, whatever the state of the control.
Required and Optional #
A required field is read from the control: the required validator, the required state of a Signal Forms field, a required or aria-required attribute; the required input overrides it. requiredIndicator sets how the label shows it: asterisk, the default, hidden from screen readers since the control gets aria-required; optional, an "(optional)" text on the other fields, for forms where most fields are required; or none.
Horizontal Layout #
With layout="horizontal", the label is beside the control, aligned with its text, and the hint and the errors stay under the control. labelWidth sets the width of the label column. When there is not enough room, the label goes back above the control.
Label Variants #
variant sets where the label goes. With top, the default, it is above the control. With over, in and on, it sits in the empty field and moves up when the control is focused, filled or has a placeholder, with the look of FloatLabel: over the field, inside it or on its border. With ifta, it stays inside the field, above the value, with the look of IftaLabel. The hint, the errors and the counter stay under the control, and the label keeps its id and its for, so screen readers read the field the same way. provideFormFieldVariant('over') sets the variant of every field below an injector.
The label goes inside text fields and the controls that look like one: InputText, Textarea, Password, InputMask, InputNumber, DatePicker, Select, MultiSelect, AutoComplete, CascadeSelect, TreeSelect, InputChips and Mention. It stays above the controls that have no room for it inside, whatever the variant: Checkbox, RadioButton, ToggleSwitch, Listbox, Slider, Knob, ColorPicker, InputOtp, PhoneInput, an InputGroup, an editor, and groups of checkboxes or radio buttons. The in-field variants ignore layout.
With Other Inputs #
FormField works with every control of the library. A native field, such as the input of a DatePicker or an InputNumber, is labelled with for; a composite one, such as the combobox of a Select, with aria-labelledby, and clicking the label focuses it. Several checkboxes or radio buttons become a group or a radiogroup named by the label, each option keeping its own label. When the control is not the first field of the content, mark it with pFormFieldControl.
Character Counter #
counter shows the number of characters under a text field, against its maxlength or the maxLength of a Signal Forms field. The visible counter is hidden from screen readers: the field is described by "Up to 160 characters", and the characters remaining are announced once the user pauses typing, not on every key.
Size and Disabled #
size scales the label, the hint and the errors; give the same size to the control. A disabled control, read from the control or forced with the disabled input, dims the label and the hint and hides the errors.
Accessibility #
Screen Reader
FormField writes the attributes that tie a control to its texts, on the focusable element of the control, and keeps the ones the control or your template already has. With no id to write:
- The control gets an id when it has none. A native field, such as an input, a textarea or the input of a DatePicker, InputNumber or MultiSelect, is named by the label element through for. A composite control, such as the combobox of a Select, gets aria-labelledby pointing to the label, unless it already has one; clicking the label focuses it.
- Several checkboxes or radio buttons are wrapped in an element with the group or radiogroup role, named by the label and described by the hint and the errors, while each option keeps its own label. A radiogroup gets aria-required and aria-invalid; a group of checkboxes, which cannot have them, gets a hidden "required" text in its name.
- aria-describedby lists the hint, the pFormFieldHint elements, the errors while they are shown and the character limit, after the ids the control already had.
- aria-invalid is set while an error is shown, not before, so a field is not read as invalid before the user filled it in. aria-required is set on a required control; the asterisk is hidden from screen readers, so "required" is not read twice.
- The errors are in a polite live region that stays in the page, without the alert role: a new error is read once, after what the screen reader is saying, and again with the description of the control when it is focused. The icon of an error is hidden, and the text, not only the color, tells the error.
- With a label inside the field, the over, in, on and ifta variants, the label is the same element with the same id, still tied to the control with for or aria-labelledby, and it comes after the control in the page; it stays visible while the field is filled, so the visible name always matches the accessible name. It does not catch clicks, which reach the control under it. A floating label moves up as soon as the control has a placeholder, so they never overlap.
- The visible character counter is hidden; the limit is read with the control, and the characters remaining are announced once the user pauses typing.
The texts are translated with the labels input or the fieldRequired, fieldOptional, characterLimit, charactersRemaining and charactersOver keys of the aria translation; the error messages with formErrors, provideFormFieldMessages or errorMessages. When the control is not the one FormField finds, the first field of the content, mark it with pFormFieldControl; a control of your own can provide FORM_FIELD_CONTROL to give its focusable element.
<!-- written -->
<p-form-field label="Email" hint="We never share it.">
<input pInputText formControlName="email" />
</p-form-field>
<!-- rendered once the field is touched with an invalid value -->
<label id="pn_id_1_label" for="pn_id_1_control">Email</label>
<input id="pn_id_1_control" aria-describedby="pn_id_1_hint pn_id_1_error" aria-invalid="true" aria-required="true" />
<div id="pn_id_1_hint">We never share it.</div>
<div id="pn_id_1_error" aria-live="polite" aria-atomic="true">Enter a valid email address.</div>Keyboard Support
FormField adds no stop to the tab order: the keyboard support is the one of the control.
| Key | Function |
|---|---|
| tab | Moves focus to the control, which reads its label, its hint and its errors. |
| shift + tab | Moves focus to the previous control; leaving a field marks it as touched and shows its errors. |