Sub topic 15
Build custom form controls
Native inputs bind to a field with [field]. Sometimes you need a different widget, such as a star rating, a date picker, or a tag input. In Signal Forms you write that widget as a component that follows a small contract, and [field] binds to it like any native input.
This chapter assumes you know how to create a form with form(), bind fields with [field], and read field state. It also assumes you are comfortable with input() and model() from Angular signals.
The control contract
A custom control is a component that exposes certain signal properties. The [field] directive finds them and wires them to the field's state.
valueis required. It must be amodel(), so the control can read the field's value and write a new one back.disabled,readonly,required,invalid, anderrorsare optional. Declare each as aninput()and the directive pushes the field's current state into it.touchedis optional. Declare it as amodel()and set it totruewhen the user leaves the control. The field then becomes touched.
The FormValueControl<T> interface describes this contract for most controls. Implementing it makes the compiler check your property types. Checkbox-like controls use FormCheckboxControl instead, with a checked model in place of value.
Example: a star rating
This control shows five buttons. Clicking one sets the value.
import { ChangeDetectionStrategy, Component, input, model } from '@angular/core';
import { FormValueControl } from '@angular/forms/signals';
@Component({
selector: 'app-star-rating',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
@for (star of stars; track star) {
<button
type="button"
[disabled]="disabled()"
[attr.aria-pressed]="star <= value()"
(click)="value.set(star)"
(blur)="touched.set(true)"
>
{{ star <= value() ? '★' : '☆' }}
</button>
}
`,
})
export class StarRating implements FormValueControl<number> {
readonly value = model(0);
readonly disabled = input(false);
readonly touched = model(false);
protected readonly stars = [1, 2, 3, 4, 5];
}The buttons use type="button" so they never submit the surrounding form. The blur handler marks the control as touched, so error messages follow the same rules as for native inputs.
Use it in a form
Import Field and the control, then bind the field as you would on an <input>.
import { Component, signal } from '@angular/core';
import { form, Field, min } from '@angular/forms/signals';
import { StarRating } from './star-rating';
@Component({
selector: 'app-review',
imports: [Field, StarRating],
template: `
<app-star-rating [field]="reviewForm.rating" />
@if (reviewForm.rating().touched() && reviewForm.rating().invalid()) {
<p class="error">Pick at least one star</p>
}
`,
})
export class ReviewComponent {
private readonly model = signal({ rating: 0 });
protected readonly reviewForm = form(this.model, (path) => {
min(path.rating, 1, { message: 'Pick at least one star' });
});
}Validators, conditional rules, and submit() all work unchanged. The form does not know the rating is a custom component.
Common mistakes
- Declaring
valueasinput()instead ofmodel(). The control can then read the value but cannot change it. - Forgetting
touched. The field never becomes touched, so errors guarded bytouched()never appear. - Omitting
type="button"on inner buttons, which makes them submit the form. - Ignoring
disabled. A field disabled by a rule would still accept clicks.
Exercise
Your custom control updates its own display when clicked, but the form model never changes. What is the most likely cause, and how do you fix it?
Show answer
The value property is probably declared with input() or as a plain property, so the control cannot write to the field. Declare it with model() and update it with value.set(...) so the change flows back to the form model.
Exercise
Which property must a custom control add so that a touched() && invalid() error check works after the user leaves it, and what type should it have?
Show answer
Add touched, declared as a model(false). Call touched.set(true) in a blur handler so the field becomes touched.