P
P
Pogran2016-12-08 14:59:07
Angular
Pogran, 2016-12-08 14:59:07

How to create an asynchronous form validator?

I want to create an asynchronous validator using an observable, but I don't know if this is possible. Now it only turns out to be done through promises, but that's not it.

//вызов 
this.userForm = fb.group({
            firstName : null,
            surname : null,
            username : null,
            email : [null, Validators.compose([Validators.required, Validators.minLength(5)]), ValidationService.uniqueEmail],
            password : '',
            role : '',
            state : '',
            phone : '',
        })

static uniqueEmail(control: FormControl) {
        return Promise.resolve().then(() => {
            setTimeout(() => {
                console.log('ttt');
                if(control.value == '[email protected]') {
                    return null;
                } else {
                    return { 'notUnique' : true };
                }
            }, 2000);
        });
    }

The point is that I want to check if the email is unique, but with promises it won't work because I want to use a delay using debounceTime(500) . The principle should be like this. The user enters data, we have a delay of 0.5 seconds, then there is a request, but if the user continues to enter data back, this request must be canceled and a new one sent. how can this be implemented? I think that only observable will help here, only how to get involved with it

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question