Sub topic 3
Create a form with form()
This chapter assumes you know TypeScript, standalone components, and signal() and computed(). You should also have read the previous chapter on the signal model. Signal Forms is experimental in Angular 21, so names can change in later releases.
What form() does
The form() function takes a writable signal and returns a form built on top of it. It does not copy the data. The signal stays the single source of truth, and the form is a view over it that adds state and validation.
import { signal } from '@angular/core';
import { form } from '@angular/forms/signals';
const model = signal({ name: '', age: 0 });
const profileForm = form(model);profileForm has the same shape as the model. It is a tree of fields, with one node for each property.
Reading the field tree
Each property of the form is itself callable. Calling a field returns its state object. Calling the root returns the state of the whole form.
import { Component, signal } from '@angular/core';
import { form } from '@angular/forms/signals';
@Component({
selector: 'app-profile',
template: `
<p>Name: {{ profileForm.name().value() }}</p>
<p>Form valid: {{ profileForm().valid() }}</p>
`,
})
export class ProfileComponent {
private readonly model = signal({ name: 'Ada', age: 36 });
protected readonly profileForm = form(this.model);
}There are two access steps. profileForm.name selects the field. profileForm.name() returns that field's state. Each state property, such as value() or valid(), is a signal, so templates and computed() values update automatically.
Adding a schema function
The second argument to form() is a schema function. It receives a path object that mirrors the model. You pass parts of that path to rules such as validators.
import { signal } from '@angular/core';
import { form, required, min } from '@angular/forms/signals';
const model = signal({ name: '', age: 0 });
const profileForm = form(model, (path) => {
required(path.name, { message: 'Name is required' });
min(path.age, 18, { message: 'You must be 18 or older' });
});The schema runs once, when the form is created. It declares rules and does not check values at that moment. The rules re-evaluate whenever the model changes.
Model and form stay in sync
Writing to the model updates the form. Writing to a field's value updates the model.
model.update((m) => ({ ...m, name: 'Grace' }));
console.log(profileForm.name().value()); // "Grace"
profileForm.name().value.set('Ada');
console.log(model().name); // "Ada"Common mistakes
- Forgetting the call parentheses.
profileForm.name.value()fails becausenameis a field, not a state object. UseprofileForm.name().value(). - Giving the model an initial value of
undefinedfor a property. Use an empty string,0, ornullso every field exists in the tree. - Defining the model type loosely. A typed model gives you autocompletion on
pathand on the field tree.
Exercise
A form is created with const f = form(signal({ city: '' })). Write the expression that reads whether the city field is valid, and explain why f.city.valid() does not work.
Show answer
Use f.city().valid(). f.city is the field, and calling it returns the field state. valid is a signal on that state object, so f.city.valid is undefined and cannot be called.