Your turn. Pause the video, and try this. Build a reactive signup form. The name is required, and the password needs eight characters. A group validator checks that the two passwords match. On submit, welcome the user. Here is one solution. The form group has three controls. The passwords match validator goes on the group. We filled in matching passwords, and signed up as Sam.
✍️ Exercise: A signup form
Reactive form: name (required), password (minLength 8), confirm. Group validator passwordsMatch. Disable "Sign up" while invalid; on submit show "Welcome, NAME!".
Show hint
new FormGroup({ ... }, { validators: passwordsMatch })
Show solution
app.tstypescript
import { Component, signal } from '@angular/core';import { ReactiveFormsModule, FormGroup, FormControl, Validators } from '@angular/forms';import { passwordsMatch } from './validators';@Component({ selector: 'app-root', imports: [ReactiveFormsModule], templateUrl: './app.html',})export class App { done = signal(''); signup = new FormGroup({ name: new FormControl('', Validators.required), password: new FormControl('', Validators.minLength(8)), confirm: new FormControl(''), }, { validators: passwordsMatch });}
Output
[DOM] Input elements should have autocomplete attributes (suggested: "new-password"):
(More info: https://goo.gl/9p2vKq) %o
[DOM] Input elements should have autocomplete attributes (suggested: "new-password"):
(More info: https://goo.gl/9p2vKq) %o
The template uses template URL, so it lives in app dot html. The mismatch message shows after the user types in the confirm field. And the button stays disabled, until the whole form is valid.
Ng model binds inputs in template driven forms. Form controls, and groups, build reactive forms in the class. Signal forms build a form from a signal. And validators, built-in or your own, check every field.