Sub topic 15
Build custom form controls
Native inputs such as <input>, <select>, and <textarea> work with the [field] binding without extra code. Real applications also need controls that have no native equivalent: star ratings, tag pickers, date-range selectors, toggle groups. This chapter shows how to write a component that the Field directive can bind to in the same way it binds to an <input>.
It assumes you know standalone components, input() and model() signal APIs, and the form() and [field] basics from earlier chapters. Signal Forms is experimental in Angular 21, so the interface names below may change in later releases.
The contract: a component with the right signals
Signal Forms does not use ControlValueAccessor for custom controls. There are no writeValue or registerOnChange callbacks. Your component declares signal members with agreed names, and Field connects them to the field state.
Angular supplies two interfaces from @angular/forms/signals that describe those members:
FormValueControl<T>is for controls that hold a value of typeT, such as text, numbers, dates, or objects.FormCheckboxControlis for controls that are on or off, such as checkboxes and switches.
The two differ in one required member. A value control must have a value model. A checkbox control must have a checked model. A control has one or the other, never both.
Everything else is optional. The Field directive checks which of those members your component has and passes data only to the ones it finds.
A minimal value control
A star rating holds a number from 0 to 5. The value model is the only required member. The disabled, readonly, and touched members are optional, and the example adds them so the control behaves like a native input.
import { Component, input, model } from '@angular/core';
import { FormValueControl } from '@angular/forms/signals';
@Component({
selector: 'app-rating',
template: `
@for (star of stars; track star) {
<button
type="button"
[disabled]="disabled() || readonly()"
[attr.aria-pressed]="star <= value()"
[attr.aria-label]="star + ' of 5'"
(click)="value.set(star)"
(blur)="touched.set(true)"
>
{{ star <= value() ? '★' : '☆' }}
</button>
}
`,
})
export class RatingComponent implements FormValueControl<number> {
readonly value = model(0);
readonly disabled = input(false);
readonly readonly = input(false);
readonly touched = model(false);
protected readonly stars = [1, 2, 3, 4, 5];
}Three details matter here.
valueis amodel(), not aninput(). A model is a signal the parent can write to and the component can write to as well.Fieldwrites the form's current value into it. When the user clicks a star,value.set(star)pushes the new value back into the form model.- The buttons use
type="button". Without it, a button inside a<form>submits the form. - The component does not store any validation state. It only reads what the field tells it.
Binding the control
Bind a custom control with the same [field] attribute you use on native inputs. Add the component and Field to the parent's imports.
import { Component, signal } from '@angular/core';
import { form, Field, required, min } from '@angular/forms/signals';
import { RatingComponent } from './rating.component';
@Component({
selector: 'app-review',
imports: [Field, RatingComponent],
template: `
<form (submit)="onSubmit($event)">
<label>
Your rating
<app-rating [field]="reviewForm.rating" />
</label>
@if (reviewForm.rating().touched() && reviewForm.rating().invalid()) {
@for (error of reviewForm.rating().errors(); track error.kind) {
<p class="error">{{ error.message }}</p>
}
}
<label>Comment <textarea [field]="reviewForm.comment"></textarea></label>
<button type="submit" [disabled]="reviewForm().invalid()">Send</button>
</form>
`,
})
export class ReviewComponent {
private readonly model = signal({ rating: 0, comment: '' });
protected readonly reviewForm = form(this.model, (path) => {
min(path.rating, 1, { message: 'Pick at least one star' });
required(path.comment, { message: 'Write a short comment' });
});
protected onSubmit(event: Event) {
event.preventDefault();
console.log(this.model());
}
}The validators are attached to path.rating in the schema, not in the rating component. The component stays reusable, and each form decides its own rules. The error list, the touched() check, and the submit button work exactly as they do for a native input.
What flows in and out
The following diagram shows the direction of each connection between the form and your component.
The state flowing in is read-only from the control's point of view, except for value (or checked) and touched, which the control updates.
Optional members the directive understands
If your component declares a member with one of these names, Field keeps it in sync. The types follow the Angular 21 interfaces, so check them against your installed version.
| Member | Kind | Purpose |
|---|---|---|
| `value` | `model()` | Required for value controls. Holds the field value. |
| `checked` | `model()` | Required for checkbox controls. Holds the on/off state. |
| `touched` | `model()` or `input()` | Set to `true` on blur so the field becomes touched. |
| `disabled` | `input()` | `true` when a rule such as `disabled()` applies. |
| `readonly` | `input()` | `true` when the field is read-only. |
| `hidden` | `input()` | `true` when the field is hidden by a rule. |
| `invalid` | `input()` | `true` when the field has errors. |
| `errors` | `input()` | The current list of validation errors. |
| `required` | `input()` | `true` when the field has a `required` rule. |
| `name` | `input()` | The field's name. |
| `min`, `max`, `minLength`, `maxLength`, `pattern` | `input()` | The limits from the matching validators. |
Declare only what the control uses. A rating component has no use for pattern.
A checkbox-style control
For a toggle, implement FormCheckboxControl and expose a checked model instead of value.
import { Component, input, model } from '@angular/core';
import { FormCheckboxControl } from '@angular/forms/signals';
@Component({
selector: 'app-switch',
template: `
<button
type="button"
role="switch"
[attr.aria-checked]="checked()"
[disabled]="disabled()"
(click)="checked.set(!checked())"
(blur)="touched.set(true)"
>
{{ checked() ? 'On' : 'Off' }}
</button>
`,
})
export class SwitchComponent implements FormCheckboxControl {
readonly checked = model(false);
readonly disabled = input(false);
readonly touched = model(false);
}Bind it to a boolean property in the model, for example <app-switch [field]="settingsForm.notifications" />. The bound field must hold a boolean.
Common errors
- Using
input()forvalue. A plain input cannot be written from inside the component, so clicks never reach the form. Usemodel(). - Forgetting to set
touched. Without a blur handler,touched()staysfalse, and any error message gated ontouched()never appears. Settouchedwhen the control loses focus, or when the user finishes interacting with it. - Ignoring
disabledandreadonly. If the control does not declare these inputs, adisabled()rule changes the field state but the control keeps accepting clicks. Declare them and apply them to the interactive elements. - Declaring both
valueandchecked. Pick one. A control that has both is ambiguous. - Binding a mismatched type. A
FormValueControl<number>bound to a string field is a type error. Match the control'sTto the model property. - Putting validation inside the control. Rules in the component cannot be changed by the form that uses it, and they bypass the field's
errors(). Keep rules in the schema function.
Accessibility and focus
A custom control replaces something the browser used to handle for you. Check these points:
- Give interactive parts real elements (
button,input) or the rightroleand keyboard handling. - Reflect state with ARIA attributes such as
aria-pressedoraria-checked, as the examples do. - Associate a visible label. Wrapping the control in
<label>, as the review example does, works when the control contains a focusable element. - If you declare an
invalidinput, bind it toaria-invalidon the focusable element so assistive technology announces the state.
Exercise
Your date-range picker component holds an object of type { start: string; end: string }. Which interface and which member do you use, and why must that member be a model() and not an input()? Also name the member you must update on blur so that error messages gated on touched() can appear.
Show answer
Implement FormValueControl<{ start: string; end: string }> and declare readonly value = model({ start: '', end: '' }). It must be a model() because the Field directive writes the form value into it, and the component also has to write user changes back, which a read-only input() cannot do. On blur, call touched.set(true) on a touched model so the field becomes touched.
Exercise
A form has a rule disabled(path.rating, ({ valueOf }) => valueOf(path.locked)), but the star buttons in your rating control still respond to clicks when locked is true. What is missing, and how do you fix it?
Show answer
The component does not declare a disabled input, or it declares one but does not apply it. Add readonly disabled = input(false) and bind it to the buttons with [disabled]="disabled()". The Field directive then passes the field's disabled state into the component.