Angular·Angular from components to production

Reactive forms

Build typed forms with synchronous validation and clear errors.

Intermediate 14 min read Lesson 7 of 300 Challenge included
What you will be able to do
  • Use fb.group and nonNullable controls.
  • Add Validators.required and Validators.email.
  • Show errors only after the control is touched.
  • Validate again on the server.

In plain English

Reactive forms model the form in the class, which makes validation and testing explicit. Use FormBuilder, nonNullable controls, Validators and a visible error message tied to the control.

signup.component.tsTYPESCRIPT
import { Component, inject } from '@angular/core'
import { NonNullableFormBuilder, ReactiveFormsModule, Validators } from '@angular/forms'

@Component({
  selector: 'app-signup',
  standalone: true,
  imports: [ReactiveFormsModule],
  template: `
    <form [formGroup]="form" (ngSubmit)="submit()">
      <label>Email <input formControlName="email" type="email"></label>
      @if (form.controls.email.touched && form.controls.email.invalid) {
        <p>Enter a valid email.</p>
      }
      <button [disabled]="form.invalid">Subscribe</button>
    </form>
  `,
})
export class SignupComponent {
  private fb = inject(NonNullableFormBuilder)
  form = this.fb.group({ email: ['', [Validators.required, Validators.email]] })
  submit() { if (this.form.valid) console.log(this.form.getRawValue()) }
}
Your turn

Add a required name control

Easy

Add a nonNullable name control with Validators.required.

TYPESCRIPT · starter
this.fb.group({ email: ['', Validators.email] })
Check your understanding before moving on.
Knowledge checkQuestion 1

What does a nonNullable control guarantee?

Select an answer to continue
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Next: Routing
Lesson 7 of 300 · 0% of course