Angular 2.3.1 asynchronous authentication credential does not allow

I have a registration form, and I want to check this if the username is already taken or not. For this, I am using promises now. My registration component is as follows:

import { Component, OnInit } from '@angular/core';
import { FormControl, FormBuilder, FormGroup, Validators } from '@angular/forms';
import { UserService } from '../shared/user.service'

@Component({
  selector: 'app-auth',
  providers: [],
  templateUrl: './auth.component.html',
  styleUrls: ['./auth.component.css']
})

export class AuthComponent implements OnInit {

  // Tabs for log in or sign up
  tab = 'signup';

  // Sign up form
  signUpForm: FormGroup;

  // Log in form
  logInForm: FormGroup;

  constructor(private formBuilder: FormBuilder, private ussr: UserService) {
     this.signUpForm = this.formBuilder.group({
       'username': [null, [
         Validators.required, Validators.minLength(4), Validators.maxLength(12), this.ussr.getUserNameFromServer
       ]], 
       'email': '', 
       'password': '' });
    this.logInForm = this.formBuilder.group({ 'username': '', 'password': '' });
  }

  ngOnInit() {
  }

  activeTab(tab: string) {
    this.tab = tab;
  }

  signUpSubmit(value: any) {
    console.log(value);
  }

}

And UserService:

import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions  } from '@angular/http';
import { FormControl } from '@angular/forms';
import 'rxjs/add/operator/map';

@Injectable()
export class UserService {

    constructor (private http: Http) {}

    private extractData (res: Response) {
        let body = res.json();
        return body || { };
    }

    getUserNameFromServer = (c: FormControl) => {

        return new Promise (
             (resolve, reject) => {
                 this.http.get('https://jsonplaceholder.typicode.com/users/1')
                 .map(this.extractData)
                 .subscribe(
                     (res: any) => {
                         if (c.value == res.username) {
                             console.log('taken')
                             resolve({'usernameTaken': true})
                         } else {
                             console.log('is not taken')
                             resolve(null)
                         }
                     },
                     err => { console.log(err) }
                 )   
             }
         );

    }

}

I already read some blog posts on this topic, and I also checked two SO ( 1 , 2 ) questions , but I can't get it to work. The service successfully received a server response, but when I call it inside the component validator, the form will be invalid each time.

In the examples above, they just call it in the validator section, and I assume that ng2 does the rest of the work in the background, or am I mistaken? How did the validator get the promise value?

+4
1

, . , , .

:

'username': [null,
         [ Validators.required, Validators.minLength(4), Validators.maxLength(12) ],
         [ this.ussr.getUserNameFromServer ]
  ],
+6

Source: https://habr.com/ru/post/1665564/


All Articles