Reactive forms
Build typed forms with synchronous validation and clear errors.
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.
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
EasyAdd a required name control
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 inFinished reading?
Lock in what you just learned.
0%
Mark this lesson complete to save your place and move one step closer to the certificate.