0

I want to make a custom async validator in my Angular project.

I'm using firestore for my DB, and the structure of it is

colloection (profiles) - doc ( email, username )

this is my form code,

   this.registerForm = this.formBuilder.group({
      email : ['', Validators.compose([Validators.required, Validators.email]), this.emailCheck.bind(this) ],
      password : ['', Validators.required],
      confirmPassword : ['', Validators.compose([Validators.required, this.isEqualPassword.bind(this)])]
  })

but i don't know how to make this.emailCheck.bind(this) function.

what I did is

emailCheck(control: FormControl)  {
  control.valueChanges
      .debounceTime(1500)
      .switchMap(val => this.emailValid(val))
      .subscribe((res) => {
        return res ? null : {emailAvailable : true}
      })



 emailValid(val) : Observable<any> {
  let ref = this.afs.collection(`profiles`, ref => ref.where('email', '==', val))
  return ref.valueChanges()

and then I get error

enter image description here

please let me know how to fix this.

1

1 Answer 1

1

Check official documenation on how to create custom validation on angular

Basically what you have to do is create a function that returns a Promise or Observable with the errors or null.

emailCheck(control: FormControl): Observable<{[key: string]: any}>  {
  return control.valueChanges
      .debounceTime(1500)
      .switchMap(val => this.emailValid(val))
      .map(res => res ? null : {emailAvailable : true})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.