Angular13 min read

Angular Forms: Template-Driven and Reactive Forms

Master Angular forms - template-driven and reactive forms. Learn form validation, handling user input, and building complex forms. Essential for collecting and validating user data.

Lisa Anderson
December 18, 2025
0.0k0

Forms are how users interact with your application. Angular provides two approaches - template-driven and reactive forms. Understanding both helps you choose the right approach for your needs.

Template-Driven Forms

Template-driven forms use directives in templates (ngModel, ngForm). They're simpler for basic forms but less flexible. Angular handles form state automatically.

Reactive Forms

Reactive forms use FormBuilder and FormControls defined in the component class. They're more powerful, testable, and flexible. Recommended for complex forms.

Form Validation

Angular provides built-in validators (required, email, min, max) and supports custom validators. Show validation messages and disable submission until valid.

Handling Form Data

Access form values, handle submission, reset forms, and manage form state. I'll show you how to work with form data effectively.

Dynamic Forms

Build forms dynamically based on data or user input. Reactive forms make this easier with FormArray and dynamic controls.

Best Practices

I'll show you when to use each approach, how to structure forms, and validation patterns. These skills are essential for building production-ready forms.

#Angular#Forms#Reactive Forms#Validation

Common Questions & Answers

Q1

What's the difference between template-driven and reactive forms?

A

Template-driven forms use directives (ngModel) in templates, form state managed by Angular. Reactive forms use FormBuilder/FormControl in component class, more programmatic control. Reactive forms are more testable, flexible, and recommended for complex forms. Template-driven are simpler for basic forms.

typescript
// Template-driven form
<form #myForm="ngForm" (ngSubmit)="onSubmit(myForm)">
  <input name="email" ngModel required email>
  <input name="password" ngModel required minlength="6">
  <button type="submit" [disabled]="!myForm.valid">Submit</button>
</form>

// Reactive form
import { FormBuilder, FormGroup, Validators } from '@angular/forms';

export class LoginComponent {
  loginForm: FormGroup;
  
  constructor(private fb: FormBuilder) {
    this.loginForm = this.fb.group({
      email: ['', [Validators.required, Validators.email]],
      password: ['', [Validators.required, Validators.minLength(6)]]
    });
  }
  
  onSubmit() {
    if (this.loginForm.valid) {
      console.log(this.loginForm.value);
    }
  }
}
Q2

How do I validate Angular forms?

A

Use built-in validators (Validators.required, Validators.email, Validators.minLength) or create custom validators. Check form.valid or control.valid, display error messages based on control.errors. Use validators in reactive forms or validation directives in template-driven.

typescript
import { FormBuilder, Validators, AbstractControl } from '@angular/forms';

// Built-in validators
this.form = this.fb.group({
  email: ['', [Validators.required, Validators.email]],
  password: ['', [Validators.required, Validators.minLength(8)]]
});

// Custom validator
function passwordStrength(control: AbstractControl) {
  const value = control.value;
  if (!value) return null;
  
  const hasNumber = /[0-9]/.test(value);
  const hasUpper = /[A-Z]/.test(value);
  const hasLower = /[a-z]/.test(value);
  
  const valid = hasNumber && hasUpper && hasLower;
  return valid ? null : { passwordStrength: true };
}

// Use custom validator
password: ['', [Validators.required, passwordStrength]]

// In template
<input formControlName="email">
<div *ngIf="form.get('email')?.hasError('required')">
  Email is required
</div>