1

Stackblitz link to see code

  1. I want to use a custom Async validator in my code to check if certain pin-codes exist in my database or not and then take some action in the HTML reactive form
  2. My HTML element instantly calls the method before the validator is done validating so the angular make the "pending" status true as the validation is pending

What I want??

  • I want the html to call my method after all the validators are done evaluating -->do not suggest to use set timeout

if you can help me with the proper way of doing this it will be great.

Currently I am using settimeout function inside my method and it is working fine but it is not the correct way.

1 Answer 1

1
  1. Remove this line from the template
(input)="userForm.get('pinCode').value != '' ? checkForPincode() : null"

this is responsible for checking calling checkForPincode() as soon as the user presses a key.

  1. Forms, and fields, have various event streams attached to them, I think statusChanges will be the best. You can, after the form is created, do
this.userForm.get('pinCode').statusChanges
    .subscribe(() => this.checkForPincode());

statusChanges can be things like VALID, INVALID, PENDING.

To save yourself unnecessary checks, you can pass it through distictUntilChanged operator

this.userForm.get('pinCode').statusChanges
    .pipe(
        distictUntilChanged(),
    ).subscribe(() => this.checkForPincode());

or even do the check for field validity right there:

this.userForm.get('pinCode').statusChanges
    .pipe(
        distictUntilChanged(),
        filter(value => value === 'VALID'),
    ).subscribe(() => this.userForm.get('Province').enable());
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.