Angular Asynchronous Form Validators

April 7, 2025 6:33 PM

Angular Validators Asynchronous validators
post image

The main difference between synchronous and asynchronous validators in Angular lies in how they execute and what they return. Let me explain this in simple terms:

Synchronous Validators

  • Execution: Synchronous validators run immediately when the form control's value changes.
  • Return Type: They return a validation result (ValidationErrors | null) directly.
  • Use Case: Use synchronous validators for checks that can be performed instantly, such as checking if a value is required, validating the length of a string, or matching a pattern.


The passwordStrengthValidator is a synchronous validator because it performs all checks (e.g., length, uppercase, lowercase, numeric) immediately and returns the result.

https://gist.github.com/amrohit/95a50e219d02dce3db85ade9265b2a0f

  • How It Works: The validator checks the value and immediately returns either null (valid) or an object with errors (invalid).
  • When to Use: For quick, synchronous checks like required fields, length, or regex patterns


Asynchronous Validators

  • Execution: Asynchronous validators run after synchronous validators and are designed to handle operations that take time, such as making HTTP requests.
  • Return Type: They return an Observable that emits the validation result (ValidationErrors | null).
  • Use Case: Use asynchronous validators for checks that require external data or delayed responses, such as checking if a username is already taken by querying a server.

Example: Asynchronous Validator

The usernameValidator is an asynchronous validator because it makes an HTTP request to check if the username already exists.

https://gist.github.com/amrohit/c2e5fdf7af26f68da762f1373244a543

  • How It Works: The validator sends a request to the server and waits for the response. Once the response is received, it emits either null (valid) or an error object (invalid).
  • When to Use: For operations that involve external data, like checking for duplicate usernames or validating data against a database.

How They Work Together

When both synchronous and asynchronous validators are used on the same control:

  1. Angular runs the synchronous validators first.
  2. If the synchronous validators pass (i.e., return null), Angular then runs the asynchronous validators.
  3. If any validator (synchronous or asynchronous) fails, the control is marked as invalid

https://gist.github.com/amrohit/9a7b5e079f7f45add025024d50ee3c3c

  • Synchronous Validators: Validators.required and Validators.minLength(3) ensure the username is not empty and has at least 3 characters.
  • Asynchronous Validator: usernameValidator checks with the server if the username is already taken

The main difference between synchronous and asynchronous validators is how they execute and what they return. Synchronous validators are quick and run immediately, while asynchronous validators handle delayed operations like server requests. Both are essential for building robust and efficient forms in Angular. Let me know if you need further clarification

Comments


    Read next