Skip to content

Instantly share code, notes, and snippets.

@andrewarosario
Created December 18, 2023 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewarosario/3a9cf3c89e22d35e595d449deab79201 to your computer and use it in GitHub Desktop.
Save andrewarosario/3a9cf3c89e22d35e595d449deab79201 to your computer and use it in GitHub Desktop.
import { AsyncValidatorFn, FormControl } from '@angular/forms';
import { map } from 'rxjs/operators';
export function uniqueNameValidator(userService: UserService): AsyncValidatorFn {
return (control: FormControl<string>) => {
return userService.getByName(control.value).pipe(
map((isTaken) => (isTaken ? { uniqueName: true } : null)),
);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment