0

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?

2 Answers 2

2

Async validator should be at index 2

control: ['', null, this.passwordValid.bind(this)]
Sign up to request clarification or add additional context in comments.

Comments

2

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:

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.