0 day streak
javascript / intermediate
Snippet

Validating Asynchronous Data

Async validators allow you to perform validation logic that requires a remote call, such as checking if a username is already taken. Setting 'updateOn: blur' optimizes performance by only running the check when the user leaves the field.

snippet.js
javascript
1
2
3
4
5
6
this.form = new FormGroup({
username: new FormControl('', {
asyncValidators: [this.uniqueUsernameValidator.bind(this)],
updateOn: 'blur'
})
});
angular
Breakdown
1
asyncValidators: [...]
A list of functions that return a Promise or Observable for validation.
2
updateOn: 'blur'
Configures the control to only run validators when the input loses focus.