Reactive Forms: FormControl, FormGroup and FormBuilder

Angular 17 min min read Updated: Nov 16, 2026 Intermediate
Reactive Forms: FormControl, FormGroup and FormBuilder
Intermediate Topic 2 of 5

Why Reactive Forms?

The form is defined in TypeScript, so it is easy to test, reuse and change at run time. The template only displays it.

Registration Form

typescript import { Component, inject } from '@angular/core'; import { ReactiveFormsModule, FormBuilder, Validators } from '@angular/forms'; @Component({ selector: 'app-register', imports: [ReactiveFormsModule], templateUrl: './register.html', }) export class Register { private fb = inject(FormBuilder); form = this.fb.nonNullable.group({ name: ['', [Validators.required, Validators.minLength(2)]], email: ['', [Validators.required, Validators.email]], age: [18, [Validators.min(13), Validators.max(120)]], address: this.fb.nonNullable.group({ city: ['', Validators.required], zip: ['', [Validators.required, Validators.pattern(/^\d{5}$/)]], }), newsletter: [true], }); submit() { if (this.form.invalid) { this.form.markAllAsTouched(); // show all errors return; } const value = this.form.getRawValue(); // fully typed object console.log(value.address.city, value.age); } }
html <form [formGroup]="form" (ngSubmit)="submit()" novalidate> <input formControlName="name" placeholder="Name"> @if (form.controls.name.touched && form.controls.name.errors) { <small class="error"> @if (form.controls.name.hasError('required')) { Name is required } @if (form.controls.name.hasError('minlength')) { Too short } </small> } <input formControlName="email" type="email" placeholder="Email"> <input formControlName="age" type="number"> <div formGroupName="address"> <input formControlName="city" placeholder="City"> <input formControlName="zip" placeholder="ZIP"> </div> <label><input type="checkbox" formControlName="newsletter"> Send me news</label> <button [disabled]="form.invalid">Register</button> </form>
nonNullable: fb.nonNullable makes controls reset to their initial value instead of null and gives better types: form.value.name is a string.

Without FormBuilder

typescript form = new FormGroup({ email: new FormControl('', { nonNullable: true, validators: [Validators.required, Validators.email] }), password: new FormControl('', { nonNullable: true, validators: Validators.required }), });

Useful Methods

MethodDoes
form.value / getRawValue()Current values (raw includes disabled controls)
setValue({...})Set all controls (must match the full shape)
patchValue({...})Set some controls
reset()Back to initial values and pristine state
control.disable() / enable()Turn controls off or on
markAllAsTouched()Show errors on all fields
control.valueChangesObservable of every change
control.statusChangesObservable of valid/invalid changes

Load Existing Data into the Form

typescript gOnInit() { this.userService.get(this.id()).subscribe(user => { this.form.patchValue({ name: user.name, email: user.email }); }); }

React to Changes

typescript this.form.controls.country.valueChanges .pipe(takeUntilDestroyed()) .subscribe(country => { const zip = this.form.controls.address.controls.zip; country === 'US' ? zip.addValidators(Validators.required) : zip.removeValidators(Validators.required); zip.updateValueAndValidity(); });
Reusable template for errors: Create a small FieldError component that receives a control and shows messages. This avoids repeating @if blocks in every form.

Conclusion

Reactive forms define controls in code with FormBuilder, connect them with formGroup and formControlName and give typed values. Use patchValue to load data and markAllAsTouched on submit.

Quick Summary: fb.nonNullable.group({...}) + [formGroup] + formControlName.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators