uksz
uksz

Reputation: 18699

Async Validator never resolves to true

I have created the following validation function:

passwordValid(control:Control):{ [key: string]: any; } {
    clearTimeout(this.timer);
    if (control.value){
        let q = new Promise((resolve) => {
            this.timer = setTimeout(()=>{
                this._http.post('/check', control.value)
                    .subscribe(
                        success=>{
                            resolve(null);
                        },
                        error=>{
                            resolve({'invalid': true});
                        })
            },1000);
        });
        return Observable.fromPromise(q);
    };
}

When I hook it to the control like this:

control: ['', this.passwordValid.bind(this)]

It never changes control validaiton to 'valid'. It is always invalid. What am I doing wrong?

Upvotes: 0

Views: 139

Answers (2)

Ankit Singh
Ankit Singh

Reputation: 24945

Async validator should be at index 2

control: ['', null, this.passwordValid.bind(this)]

Upvotes: 2

Thierry Templier
Thierry Templier

Reputation: 202138

With your code, you register your validator as a synchronous one (second parameter of the control).

For asynchronous ones, you need to use the third parameter:

control: ['', null, this.passwordValid.bind(this)]

This article could interest you:

Upvotes: 2

Related Questions