Skip to the page

Forms, routing and HTTP · core

Reactive, template-driven and typed forms with validators

Reactive forms keep the model in the class and are synchronous and typed. Know value versus getRawValue() and how to write cross-field and async validators.

The notes

What you must know

  • Template-driven: ngModel in the template; the model is built by directives and updates asynchronously. Quick for small forms, harder to test and build dynamically.
  • Reactive: FormGroup, FormControl, FormArray in the class; synchronous access, strongly typed since v14, valueChanges as an Observable.
  • form.value leaves out disabled controls. Use form.getRawValue() to include them, so a disabled pan field is not lost.
  • fb.group gives FormControl<string | null> because reset() sets null by default. form.value is Partial<...> because disabled controls are missing.
  • A cross-field validator is a ValidatorFn on the group: it returns null when valid or an error object such as { mismatch: true }.
  • An async validator is an AsyncValidatorFn, for example usernameFree(api); the control is PENDING while it runs.
  • Debounce async validators so you do not call the server on every key.

The red pen

Common traps: mistake → fix

  • Mistake: form.value always has every field

    Fix: disabled controls are missing.

  • Mistake: Template-driven forms are synchronous

    Fix: the model updates asynchronously.

Easy to hard

Questions interviewers ask, easy to hard

  1. Reactive or template-driven forms: how do you choose?
  2. Why is my disabled field missing from form.value?
  3. How do you write a "passwords match" validator?
  4. Why is the control type string | null?

Practise

Practise with these questions

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview