# Angular FormField Component

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.

## 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.

```typescript
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { FormFieldModule } from '@exhaustive-ui/core/formfield';
import { InputTextModule } from '@exhaustive-ui/core/inputtext';

@Component({
    template: `
        <div class="card flex justify-center">
            <p-form-field label="Email" hint="We never share it." class="w-full sm:w-80">
                <input pInputText type="email" autocomplete="email" [(ngModel)]="email" class="w-full" />
            </p-form-field>
        </div>
    `,
    standalone: true,
    imports: [FormsModule, FormFieldModule, InputTextModule]
})
export class FormfieldBasicDemo {
    email = '';
}
```

## 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.

```typescript
import { Component } from '@angular/core';
import { FormFieldModule } from '@exhaustive-ui/core/formfield';
import { InputTextModule } from '@exhaustive-ui/core/inputtext';
import { PasswordModule } from '@exhaustive-ui/core/password';

@Component({
    template: `
        <div class="card flex flex-col items-center gap-6">
            <p-form-field label="Website" class="w-full sm:w-80">
                <input pInputText type="url" placeholder="https://" class="w-full" />
                <ng-template #hint>Shown on your <strong>public</strong> profile.</ng-template>
            </p-form-field>
            <p-form-field label="Password" class="w-full sm:w-80">
                <p-password [feedback]="false" [toggleMask]="true" fluid />
                <ul pFormFieldHint class="list-disc ps-5 m-0">
                    <li>At least 12 characters</li>
                    <li>Not a password you use elsewhere</li>
                </ul>
            </p-form-field>
        </div>
    `,
    standalone: true,
    imports: [FormFieldModule, InputTextModule, PasswordModule]
})
export class FormfieldHintDemo {
}
```

## 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.

```typescript
import { Component, inject } from '@angular/core';
import { FormControl, FormGroup, FormGroupDirective, ReactiveFormsModule, Validators } from '@angular/forms';
import { MessageService } from '@exhaustive-ui/core/api';
import { ButtonModule } from '@exhaustive-ui/core/button';
import { FormFieldModule } from '@exhaustive-ui/core/formfield';
import { InputTextModule } from '@exhaustive-ui/core/inputtext';
import { ToastModule } from '@exhaustive-ui/core/toast';

@Component({
    template: `
        <p-toast />
        <div class="card flex justify-center">
            <form [formGroup]="profileForm" (ngSubmit)="onSubmit(formDirective)" #formDirective="ngForm" class="flex flex-col gap-5 w-full sm:w-80">
                <p-form-field label="Name">
                    <input pInputText formControlName="name" autocomplete="name" class="w-full" />
                </p-form-field>
                <p-form-field label="Email" hint="For the receipt only.">
                    <input pInputText type="email" formControlName="email" autocomplete="email" class="w-full" />
                </p-form-field>
                <button pButton severity="secondary" type="submit" class="self-start"><span pButtonLabel>Submit</span></button>
            </form>
        </div>
    `,
    standalone: true,
    imports: [ReactiveFormsModule, FormFieldModule, InputTextModule, ButtonModule, ToastModule],
    providers: [MessageService]
})
export class FormfieldReactiveformsDemo {
    messageService = inject(MessageService);

    profileForm = new FormGroup({
        name: new FormControl('', [Validators.required, Validators.minLength(3)]),
        email: new FormControl('', [Validators.required, Validators.email])
    });

    onSubmit(formDirective: FormGroupDirective) {
        if (this.profileForm.valid) {
            this.messageService.add({ severity: 'success', summary: 'Success', detail: 'Form is submitted', life: 3000 });
            formDirective.resetForm();
        }
    }
}
```

## Errors: 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.

```typescript
import { Component, inject } from '@angular/core';
import { FormsModule, NgForm } from '@angular/forms';
import { MessageService } from '@exhaustive-ui/core/api';
import { ButtonModule } from '@exhaustive-ui/core/button';
import { FormFieldModule } from '@exhaustive-ui/core/formfield';
import { InputTextModule } from '@exhaustive-ui/core/inputtext';
import { ToastModule } from '@exhaustive-ui/core/toast';

@Component({
    template: `
        <p-toast />
        <div class="card flex justify-center">
            <form #signupForm="ngForm" (ngSubmit)="onSubmit(signupForm)" class="flex flex-col gap-5 w-full sm:w-80">
                <p-form-field label="Username">
                    <input pInputText name="username" [(ngModel)]="user.username" required minlength="3" autocomplete="username" class="w-full" />
                </p-form-field>
                <p-form-field label="Postal code" hint="Five digits.">
                    <input pInputText name="postalCode" [(ngModel)]="user.postalCode" required pattern="[0-9]{5}" inputmode="numeric" autocomplete="postal-code" class="w-full" />
                </p-form-field>
                <button pButton severity="secondary" type="submit" class="self-start"><span pButtonLabel>Submit</span></button>
            </form>
        </div>
    `,
    standalone: true,
    imports: [FormsModule, FormFieldModule, InputTextModule, ButtonModule, ToastModule],
    providers: [MessageService]
})
export class FormfieldTemplatedrivenformsDemo {
    messageService = inject(MessageService);

    user = { username: '', postalCode: '' };

    onSubmit(form: NgForm) {
        if (form.valid) {
            this.messageService.add({ severity: 'success', summary: 'Success', detail: 'Form is submitted', life: 3000 });
            form.resetForm();
        }
    }
}
```

## Errors: 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.

```typescript
import { Component, inject, signal } from '@angular/core';
import { email, form, FormField, minLength, required, submit } from '@angular/forms/signals';
import { MessageService } from '@exhaustive-ui/core/api';
import { ButtonModule } from '@exhaustive-ui/core/button';
import { FormFieldModule } from '@exhaustive-ui/core/formfield';
import { InputTextModule } from '@exhaustive-ui/core/inputtext';
import { ToastModule } from '@exhaustive-ui/core/toast';

@Component({
    template: `
        <p-toast />
        <div class="card flex justify-center">
            <form novalidate (submit)="onSubmit($event)" class="flex flex-col gap-5 w-full sm:w-80">
                <p-form-field label="Name">
                    <input pInputText [formField]="contactForm.name" autocomplete="name" class="w-full" />
                </p-form-field>
                <p-form-field label="Email">
                    <input pInputText type="email" [formField]="contactForm.email" autocomplete="email" class="w-full" />
                </p-form-field>
                <button pButton severity="secondary" type="submit" class="self-start"><span pButtonLabel>Submit</span></button>
            </form>
        </div>
    `,
    standalone: true,
    imports: [FormField, FormFieldModule, InputTextModule, ButtonModule, ToastModule],
    providers: [MessageService]
})
export class FormfieldSignalformsDemo {
    messageService = inject(MessageService);

    model = signal({ name: '', email: '' });

    contactForm = form(this.model, (path) => {
        required(path.name, { message: 'Tell us your name.' });
        minLength(path.name, 2);
        required(path.email, { message: 'An email lets us answer you.' });
        email(path.email);
    });

    onSubmit(event: Event) {
        event.preventDefault();
        submit(this.contactForm, async () => {
            this.messageService.add({ severity: 'success', summary: 'Success', detail: 'Form is submitted', life: 3000 });
            this.contactForm().reset({ name: '', email: '' });
        });
    }
}
```

## 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.

```typescript
import { Component } from '@angular/core';
import { FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
import { ButtonModule } from '@exhaustive-ui/core/button';
import { FormFieldModule } from '@exhaustive-ui/core/formfield';
import { InputTextModule } from '@exhaustive-ui/core/inputtext';

@Component({
    template: `
        <div class="card flex justify-center">
            <form [formGroup]="modesForm" class="grid grid-cols-1 sm:grid-cols-2 gap-5 w-full md:w-[40rem]">
                <p-form-field label="Touched">
                    <input pInputText formControlName="touched" class="w-full" />
                </p-form-field>
                <p-form-field label="Dirty" showErrors="dirty">
                    <input pInputText formControlName="dirty" class="w-full" />
                </p-form-field>
                <p-form-field label="Submitted" showErrors="submitted">
                    <input pInputText formControlName="submitted" class="w-full" />
                </p-form-field>
                <p-form-field label="Always, all errors" showErrors="always" [showAllErrors]="true">
                    <input pInputText formControlName="always" class="w-full" />
                </p-form-field>
                <button pButton severity="secondary" type="submit" class="self-start"><span pButtonLabel>Submit</span></button>
            </form>
        </div>
    `,
    standalone: true,
    imports: [ReactiveFormsModule, FormFieldModule, InputTextModule, ButtonModule]
})
export class FormfieldShowerrorsDemo {
    modesForm = new FormGroup({
        touched: new FormControl('', [Validators.required, Validators.minLength(4)]),
        dirty: new FormControl('', [Validators.required, Validators.minLength(4)]),
        submitted: new FormControl('', [Validators.required, Validators.minLength(4)]),
        always: new FormControl('ab', [Validators.minLength(4), Validators.pattern(/^\d+$/)])
    });
}
```

## 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.

```typescript
import { Component, signal } from '@angular/core';
import { FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
import { ButtonModule } from '@exhaustive-ui/core/button';
import { FormFieldModule, provideFormFieldMessages } from '@exhaustive-ui/core/formfield';
import { InputTextModule } from '@exhaustive-ui/core/inputtext';

@Component({
    template: `
        <div class="card flex justify-center">
            <form [formGroup]="accountForm" (ngSubmit)="checkAvailability()" class="flex flex-col gap-5 w-full sm:w-96">
                <p-form-field label="Handle" [error]="serverError()" [errorMessages]="{ pattern: 'Use lowercase letters, digits and dashes.' }">
                    <input pInputText formControlName="handle" autocomplete="off" class="w-full" />
                </p-form-field>
                <p-form-field label="Display name">
                    <input pInputText formControlName="displayName" autocomplete="nickname" class="w-full" />
                    <ng-template pFormFieldError="maxlength" let-params>
                        Keep it under <strong>{{ params.requiredLength }}</strong> characters.
                    </ng-template>
                </p-form-field>
                <button pButton severity="secondary" type="submit" class="self-start"><span pButtonLabel>Check availability</span></button>
            </form>
        </div>
    `,
    standalone: true,
    imports: [ReactiveFormsModule, FormFieldModule, InputTextModule, ButtonModule],
    providers: [provideFormFieldMessages({
            required: '{label} is required.',
            minlength: '{label} needs at least {requiredLength} characters, {actualLength} so far.'
        })]
})
export class FormfieldMessagesDemo {
    serverError = signal<string | undefined>(undefined);

    accountForm = new FormGroup({
        handle: new FormControl('', [Validators.required, Validators.minLength(3), Validators.pattern(/^[a-z0-9-]+$/)]),
        displayName: new FormControl('Ada', Validators.maxLength(20))
    });

    constructor() {
        this.accountForm.controls.handle.valueChanges.subscribe(() => this.serverError.set(undefined));
    }

    checkAvailability() {
        if (this.accountForm.valid) {
            this.serverError.set(`"${this.accountForm.controls.handle.value}" is already taken.`);
        }
    }
}
```

## 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 .

```typescript
import { Component } from '@angular/core';
import { FormFieldModule } from '@exhaustive-ui/core/formfield';
import { InputTextModule } from '@exhaustive-ui/core/inputtext';

@Component({
    template: `
        <div class="card flex flex-col items-center gap-8">
            <div class="flex flex-col gap-5 w-full sm:w-80">
                <p-form-field label="First name">
                    <input pInputText required autocomplete="given-name" class="w-full" />
                </p-form-field>
                <p-form-field label="Company">
                    <input pInputText autocomplete="organization" class="w-full" />
                </p-form-field>
            </div>
            <div class="flex flex-col gap-5 w-full sm:w-80">
                <p-form-field label="Last name" requiredIndicator="optional">
                    <input pInputText required autocomplete="family-name" class="w-full" />
                </p-form-field>
                <p-form-field label="Job title" requiredIndicator="optional">
                    <input pInputText autocomplete="organization-title" class="w-full" />
                </p-form-field>
            </div>
        </div>
    `,
    standalone: true,
    imports: [FormFieldModule, InputTextModule]
})
export class FormfieldRequiredDemo {
}
```

## 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.

```typescript
import { Component } from '@angular/core';
import { FormFieldModule } from '@exhaustive-ui/core/formfield';
import { InputTextModule } from '@exhaustive-ui/core/inputtext';
import { SelectModule } from '@exhaustive-ui/core/select';

@Component({
    template: `
        <div class="card flex justify-center">
            <div class="flex flex-col gap-5 w-full md:w-[34rem]">
                <p-form-field label="Street" layout="horizontal">
                    <input pInputText autocomplete="street-address" class="w-full" />
                </p-form-field>
                <p-form-field label="City" layout="horizontal" hint="Where the parcel is delivered.">
                    <input pInputText autocomplete="address-level2" class="w-full" />
                </p-form-field>
                <p-form-field label="Country" layout="horizontal">
                    <p-select [options]="countries" placeholder="Select a country" class="w-full" />
                </p-form-field>
            </div>
        </div>
    `,
    standalone: true,
    imports: [FormFieldModule, InputTextModule, SelectModule]
})
export class FormfieldHorizontalDemo {
    countries = ['Canada', 'France', 'Germany', 'Japan', 'United States'];
}
```

## 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 .

```typescript
import { Component } from '@angular/core';
import { FormControl, FormGroup, FormsModule, ReactiveFormsModule, Validators } from '@angular/forms';
import { DatePickerModule } from '@exhaustive-ui/core/datepicker';
import { FormFieldModule, FormFieldVariant } from '@exhaustive-ui/core/formfield';
import { InputNumberModule } from '@exhaustive-ui/core/inputnumber';
import { InputTextModule } from '@exhaustive-ui/core/inputtext';
import { SelectModule } from '@exhaustive-ui/core/select';
import { SelectButtonModule } from '@exhaustive-ui/core/selectbutton';
import { TextareaModule } from '@exhaustive-ui/core/textarea';

@Component({
    template: `
        <div class="card flex flex-col items-center gap-6">
            <p-select-button [options]="variants" [(ngModel)]="variant" [ngModelOptions]="{ standalone: true }" optionLabel="label" optionValue="value" [allowEmpty]="false" ariaLabelledBy="label-variant-title" />
            <span id="label-variant-title" class="sr-only">Label variant</span>
            <form [formGroup]="bookingForm" class="grid grid-cols-1 md:grid-cols-2 gap-x-6 gap-y-5 w-full md:w-[40rem]">
                <p-form-field label="Full name" hint="As on your passport." [variant]="variant">
                    <input pInputText formControlName="name" autocomplete="name" class="w-full" />
                </p-form-field>
                <p-form-field label="Email" showErrors="always" [variant]="variant">
                    <input pInputText formControlName="email" type="email" autocomplete="email" class="w-full" />
                </p-form-field>
                <p-form-field label="Room" [variant]="variant">
                    <p-select formControlName="room" [options]="rooms" [showClear]="true" class="w-full" />
                </p-form-field>
                <p-form-field label="Arrival" [variant]="variant">
                    <p-date-picker formControlName="arrival" [showIcon]="true" fluid />
                </p-form-field>
                <p-form-field label="Guests" [variant]="variant">
                    <p-input-number formControlName="guests" [min]="1" [max]="6" fluid />
                </p-form-field>
                <p-form-field label="Country" [variant]="variant">
                    <p-select formControlName="country" [options]="countries" placeholder="Select a country" class="w-full" />
                </p-form-field>
                <p-form-field label="Requests" [variant]="variant" class="md:col-span-2">
                    <textarea pTextarea formControlName="requests" rows="3" class="w-full"></textarea>
                </p-form-field>
            </form>
        </div>
    `,
    standalone: true,
    imports: [FormsModule, ReactiveFormsModule, FormFieldModule, InputTextModule, SelectModule, SelectButtonModule, DatePickerModule, InputNumberModule, TextareaModule]
})
export class FormfieldLabelvariantsDemo {
    variant: FormFieldVariant = 'over';

    variants: { label: string; value: FormFieldVariant }[] = [
        { label: 'Top', value: 'top' },
        { label: 'Over', value: 'over' },
        { label: 'In', value: 'in' },
        { label: 'On', value: 'on' },
        { label: 'Ifta', value: 'ifta' }
    ];

    rooms = ['Single', 'Double', 'Suite'];

    countries = ['Canada', 'France', 'Germany', 'Japan', 'United States'];

    bookingForm = new FormGroup({
        name: new FormControl('', Validators.required),
        email: new FormControl('ada@', [Validators.required, Validators.email]),
        room: new FormControl<string | null>('Double'),
        arrival: new FormControl<Date | null>(null),
        guests: new FormControl<number | null>(2),
        country: new FormControl<string | null>(null),
        requests: new FormControl('')
    });
}
```

## 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 .

```typescript
import { Component, inject } from '@angular/core';
import { FormControl, FormGroup, FormGroupDirective, ReactiveFormsModule, Validators } from '@angular/forms';
import { MessageService } from '@exhaustive-ui/core/api';
import { ButtonModule } from '@exhaustive-ui/core/button';
import { CheckboxModule } from '@exhaustive-ui/core/checkbox';
import { DatePickerModule } from '@exhaustive-ui/core/datepicker';
import { FormFieldModule } from '@exhaustive-ui/core/formfield';
import { InputNumberModule } from '@exhaustive-ui/core/inputnumber';
import { MultiSelectModule } from '@exhaustive-ui/core/multiselect';
import { PhoneInputModule } from '@exhaustive-ui/core/phoneinput';
import { RadioButtonModule } from '@exhaustive-ui/core/radiobutton';
import { SelectModule } from '@exhaustive-ui/core/select';
import { ToastModule } from '@exhaustive-ui/core/toast';

@Component({
    template: `
        <p-toast />
        <div class="card flex justify-center">
            <form [formGroup]="tripForm" (ngSubmit)="onSubmit(formDirective)" #formDirective="ngForm" class="grid grid-cols-1 md:grid-cols-2 gap-x-6 gap-y-5 w-full md:w-[44rem]">
                <p-form-field label="Destination">
                    <p-select formControlName="destination" [options]="cities" placeholder="Select a city" class="w-full" />
                </p-form-field>
                <p-form-field label="Departure" hint="Within the next 12 months.">
                    <p-date-picker formControlName="departure" [minDate]="today" [showIcon]="true" fluid />
                </p-form-field>
                <p-form-field label="Activities">
                    <p-multi-select formControlName="activities" [options]="activities" placeholder="Select activities" display="chip" class="w-full" />
                </p-form-field>
                <p-form-field label="Travellers">
                    <p-input-number formControlName="travellers" [min]="1" [max]="9" [showButtons]="true" fluid />
                </p-form-field>
                <p-form-field label="Phone" [errorMessages]="{ phoneNumber: 'Enter a valid phone number.' }">
                    <p-phone-input formControlName="phone" defaultCountry="FR" fluid />
                </p-form-field>
                <p-form-field label="Class">
                    @for (option of classes; track option) {
                        <div class="flex items-center gap-2">
                            <p-radio-button formControlName="travelClass" [value]="option" [inputId]="'class-' + option" />
                            <label [for]="'class-' + option">{{ option }}</label>
                        </div>
                    }
                </p-form-field>
                <p-form-field label="Extras" hint="Pick at least one." class="md:col-span-2">
                    @for (extra of extras; track extra) {
                        <div class="flex items-center gap-2">
                            <p-checkbox formControlName="extras" [value]="extra" [inputId]="'extra-' + extra" />
                            <label [for]="'extra-' + extra">{{ extra }}</label>
                        </div>
                    }
                </p-form-field>
                <button pButton severity="secondary" type="submit" class="self-start"><span pButtonLabel>Book</span></button>
            </form>
        </div>
    `,
    standalone: true,
    imports: [ReactiveFormsModule, FormFieldModule, SelectModule, MultiSelectModule, DatePickerModule, InputNumberModule, PhoneInputModule, CheckboxModule, RadioButtonModule, ButtonModule, ToastModule],
    providers: [MessageService]
})
export class FormfieldInputsDemo {
    messageService = inject(MessageService);

    today = new Date();

    cities = ['Kyoto', 'Lisbon', 'Montreal', 'Reykjavik'];

    activities = ['Hiking', 'Museums', 'Food tours', 'Cycling'];

    classes = ['Economy', 'Business'];

    extras = ['Luggage', 'Insurance', 'Meals'];

    tripForm = new FormGroup({
        destination: new FormControl<string | null>(null, Validators.required),
        departure: new FormControl<Date | null>(null, Validators.required),
        activities: new FormControl<string[]>([], Validators.required),
        travellers: new FormControl<number | null>(1, [Validators.required, Validators.min(1), Validators.max(9)]),
        phone: new FormControl<string | null>(null),
        travelClass: new FormControl<string | null>(null, Validators.required),
        extras: new FormControl<string[]>([], Validators.required)
    });

    onSubmit(formDirective: FormGroupDirective) {
        if (this.tripForm.valid) {
            this.messageService.add({ severity: 'success', summary: 'Success', detail: 'Trip is booked', life: 3000 });
            formDirective.resetForm({ travellers: 1, activities: [], extras: [] });
        }
    }
}
```

## 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.

```typescript
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { FormFieldModule } from '@exhaustive-ui/core/formfield';
import { TextareaModule } from '@exhaustive-ui/core/textarea';

@Component({
    template: `
        <div class="card flex justify-center">
            <p-form-field label="Bio" hint="Shown under your name." [counter]="true" class="w-full sm:w-96">
                <textarea pTextarea rows="4" maxlength="160" [(ngModel)]="bio" class="w-full"></textarea>
            </p-form-field>
        </div>
    `,
    standalone: true,
    imports: [FormsModule, FormFieldModule, TextareaModule]
})
export class FormfieldCounterDemo {
    bio = 'Frontend developer, accessibility nerd.';
}
```

## 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.

```typescript
import { Component } from '@angular/core';
import { FormFieldModule } from '@exhaustive-ui/core/formfield';
import { InputTextModule } from '@exhaustive-ui/core/inputtext';

@Component({
    template: `
        <div class="card flex flex-col items-center gap-5">
            <p-form-field label="Small" hint="A compact field." size="small" class="w-full sm:w-80">
                <input pInputText pSize="small" class="w-full" />
            </p-form-field>
            <p-form-field label="Large" hint="A roomy field." size="large" class="w-full sm:w-80">
                <input pInputText pSize="large" class="w-full" />
            </p-form-field>
            <p-form-field label="Disabled" hint="Not editable for now." class="w-full sm:w-80">
                <input pInputText disabled value="Locked" class="w-full" />
            </p-form-field>
        </div>
    `,
    standalone: true,
    imports: [FormFieldModule, InputTextModule]
})
export class FormfieldSizeDemo {
}
```

## 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.

## Pass Through Options

| Name | Type | Description |
|------|------|-------------|
| host | PassThroughOption<HTMLElement, I> | Used to pass attributes to the host's DOM element. |
| root | PassThroughOption<HTMLElement, I> | Used to pass attributes to the root's DOM element, the host. |
| labelContainer | PassThroughOption<HTMLDivElement, I> | Used to pass attributes to the element that holds the label. |
| label | PassThroughOption<HTMLElement, I> | Used to pass attributes to the label element. |
| required | PassThroughOption<HTMLSpanElement, I> | Used to pass attributes to the required asterisk. |
| optional | PassThroughOption<HTMLSpanElement, I> | Used to pass attributes to the optional text. |
| content | PassThroughOption<HTMLDivElement, I> | Used to pass attributes to the element that holds the control and the messages. |
| control | PassThroughOption<HTMLDivElement, I> | Used to pass attributes to the element that holds the projected control, the group of a checkbox or radio button group. |
| footer | PassThroughOption<HTMLDivElement, I> | Used to pass attributes to the element under the control that holds the hint, the errors and the counter. |
| hint | PassThroughOption<HTMLDivElement, I> | Used to pass attributes to the hint element. |
| error | PassThroughOption<HTMLDivElement, I> | Used to pass attributes to the error region, a polite live region. |
| errorMessage | PassThroughOption<HTMLDivElement, I> | Used to pass attributes to each error message. |
| errorIcon | PassThroughOption<SVGElement, I> | Used to pass attributes to the icon of an error message. |
| counter | PassThroughOption<HTMLDivElement, I> | Used to pass attributes to the character counter. |
| counterDescription | PassThroughOption<HTMLSpanElement, I> | Used to pass attributes to the hidden description of the character limit. |
| liveRegion | PassThroughOption<HTMLSpanElement, I> | Used to pass attributes to the live region that announces the characters remaining. |

## Theming

### CSS Classes

| Class | Description |
|-------|-------------|
| p-formfield | Class name of the root element |
| p-formfield-horizontal | Class name of the root element in the horizontal layout |
| p-formfield-invalid | Class name of the root element while an error is shown |
| p-formfield-disabled | Class name of the root element when the control is disabled |
| p-formfield-label-over | Class name of the root element when the label floats over the control (variant 'over') |
| p-formfield-label-in | Class name of the root element when the label floats inside the control (variant 'in') |
| p-formfield-label-on | Class name of the root element when the label floats on the border of the control (variant 'on') |
| p-formfield-label-ifta | Class name of the root element when the label is inside the control, above the value (variant 'ifta') |
| p-formfield-label-container | Class name of the element that holds the label |
| p-formfield-label | Class name of the label element |
| p-formfield-required | Class name of the required asterisk |
| p-formfield-optional | Class name of the optional text |
| p-formfield-content | Class name of the element that holds the control and the messages |
| p-formfield-control | Class name of the element that holds the projected control |
| p-formfield-footer | Class name of the element under the control |
| p-formfield-messages | Class name of the element that holds the hints and the errors |
| p-formfield-hint | Class name of the hint element |
| p-formfield-error | Class name of the error region |
| p-formfield-error-message | Class name of an error message |
| p-formfield-error-icon | Class name of the icon of an error message |
| p-formfield-counter | Class name of the character counter |
| p-formfield-counter-over | Class name of the character counter beyond the limit |

### Design Tokens

| Token | CSS Variable | Description |
|-------|--------------|-------------|
| formfield.gap | --p-formfield-gap | Gap of root, between the label and the control |
| formfield.horizontal.gap | --p-formfield-horizontal-gap | Gap of horizontal, between the label and the control |
| formfield.horizontal.label.width | --p-formfield-horizontal-label-width | Label width of horizontal |
| formfield.horizontal.label.padding.top | --p-formfield-horizontal-label-padding-top | Label padding top of horizontal, to align the label with the text of the control |
| formfield.label.color | --p-formfield-label-color | Color of label |
| formfield.label.disabled.color | --p-formfield-label-disabled-color | Disabled color of label and hint |
| formfield.label.font.weight | --p-formfield-label-font-weight | Font weight of label |
| formfield.label.font.size | --p-formfield-label-font-size | Font size of label |
| formfield.label.sm.font.size | --p-formfield-label-sm-font-size | Font size of label in a small field |
| formfield.label.lg.font.size | --p-formfield-label-lg-font-size | Font size of label in a large field |
| formfield.required.color | --p-formfield-required-color | Color of the required asterisk |
| formfield.optional.color | --p-formfield-optional-color | Color of the optional text |
| formfield.optional.font.weight | --p-formfield-optional-font-weight | Font weight of the optional text |
| formfield.message.gap | --p-formfield-message-gap | Gap of message, between the control and the hint or the errors |
| formfield.message.sm.font.size | --p-formfield-message-sm-font-size | Font size of hint, errors and counter in a small field |
| formfield.message.lg.font.size | --p-formfield-message-lg-font-size | Font size of hint, errors and counter in a large field |
| formfield.hint.color | --p-formfield-hint-color | Color of hint |
| formfield.hint.font.size | --p-formfield-hint-font-size | Font size of hint |
| formfield.error.color | --p-formfield-error-color | Color of error |
| formfield.error.font.size | --p-formfield-error-font-size | Font size of error |
| formfield.error.icon.size | --p-formfield-error-icon-size | Icon size of error |
| formfield.counter.color | --p-formfield-counter-color | Color of counter |
| formfield.counter.font.size | --p-formfield-counter-font-size | Font size of counter |
| formfield.group.gap | --p-formfield-group-gap | Gap of group, between the checkboxes or radio buttons of a group |
| formfield.invalid.border.color | --p-formfield-invalid-border-color | Border color of the controls while an error is shown |

