AI Hints
Home›Angular Tutorial›Signal Forms, Custom Validators and Practice: A Signup Form
Angular · Lesson 88 of 120

Signal Forms

Forms from a signal.

⏱ 2 min read▶ Video at 1:45:14🎓 Free
Watch this lesson in the Angular courseWatch on YouTube ↗Full Angular course

Our app keeps its data in signals. Signal forms build a form straight from a signal. They're the newest way to build forms in Angular.

  • Data in signals
  • A form from a signal
  • The newest way

How signal forms work

Start with a signal, holding the form's data. Form wraps it, and adds the rules, in a schema function. Form field, in square brackets, connects an input to one field. And each field has signals of its own, like value, valid, and errors.

keyvalue
model = signal({ ... })the data
form(model, p => { ... })the form and its rules
[formField]="signup.email"connect an input
signup.email().errors()field state, as signals

A signal form

The model is a signal, with an email. Form adds two rules, required, and email, each with its own message. The input is connected with form field. The page shows the model, and whether the form is valid. And at for lists the email's errors. We type sam at. Run it. The model signal changed as we typed. The form is invalid, and the email rule's message is showing.

app.tstypescript
import { Component, signal } from '@angular/core';
import { form, FormField, required, email } from '@angular/forms/signals';
@Component({
  selector: 'app-root',
  imports: [FormField],
  template: `
    <input [formField]="signup.email" placeholder="Email">
    <p>Model: "{{ model().email }}" · valid: {{ signup().valid() }}</p>
    @for (e of signup.email().errors(); track e.kind) {
      <p class="error">{{ e.message }}</p> }
  `,
})
export class App {
  model = signal({ email: '' });
  signup = form(this.model, p => {
    required(p.email, { message: 'Email is required' });
    email(p.email, { message: 'That is not an email' });
  });
}
Also uses actions.json
[{"type": "input", "text": "sam@"}]

Reactive or signal forms?

Reactive forms are used in most existing apps, so you'll see them often. Signal forms fit naturally with signals, and need less code. Both are good choices, and both are part of Angular.

Reactive forms

  • FormGroup, FormControlin most existing appsvery common

Signal forms

  • form(signal)fit with signalsless code

Summary

Signal forms build a form from a signal, with rules in a schema, and connect inputs with form field. Next, writing our own validators.

Key takeaways
  • form(model, ...)
  • [formField]
  • required(), email()
Next up: Custom Validators — your own rules