4

I'm applying Angular 4 in my project and I am having trouble with Custom Validation using HTTP request.

I also tried this: How to implement Custom Async Validator in Angular2.

But it doesn't work in my project.

Here's what I've done so far:

Validation biding:

    userName: ['', [Validators.required, Validators.minLength(3), this.validateUserName.bind(this)]]

Value changes event:

    let userNameControl = this.individualForm.get('userName');
userNameControl.valueChanges.subscribe(value => {
    this.setErrorMessagesForUserNameControl(userNameControl)
  }
);

Validation function:

validateUserName(control: FormControl): Promise<any> {
let promise = new Promise<any>(
  (resolve, reject) => {
    if (this.oldUserNameForCheck != undefined) {
      this._individualUpdateService.isUserNameExisting(this.oldUserNameForCheck, control.value).subscribe(
        (res) => {
          if (res.isUserNameExisting) {
            console.log("existing");
            resolve({'existing': true});
          } else {
            console.log("NOT existing");
            resolve(null);
          }
        },
        (error) => {
          console.log(error);
        }
      );
    } else {
      resolve(null);
    }
  }
);
return promise;
}

Error Messages

I just try to validate the username by sending it to the back-end.

Here's the logs

As you can see, there's a message for "required", "minlength". Those work fine. But the message for Custom validation is not quite clear.

1 Answer 1

2

Async validator should be in the next parameter

userName: ['', 
    [ Validators.required, Validators.minLength(3) ], 
    [ this.validateUserName.bind(this) ] 
]

Also its better to have a factory method with required dependencies that would create validator, instead of using 'bind(this)'

userNameValidator(originalUsername, individualUpdateService) {
    return (control: FormControl): Promise<any> => {
        return new Promise<any>((resolve, reject) => {
            if (originalUsername != undefined) {
                individualUpdateService.isUserNameExisting(originalUsername, control.value).subscribe(
                    (res) => {
                        if (res.isUserNameExisting) {
                            console.log("existing");
                            resolve({ 'existing': true });
                        } else {
                            console.log("NOT existing");
                            resolve(null);
                        }
                    },
                    (error) => {
                        console.log(error);
                    }
                );
            } else {
                resolve(null);
            }
        })
    }
}

userName: ['', 
    [ Validators.required, Validators.minLength(3) ], 
    [ this.userNameValidator(this.oldUserNameForCheck, this._individualUpdateService) ] 
]
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your help, but it doesn't work. Here's a log printed out on console: dropbox.com/s/19acvswgf1951s9/2017-04-19_160732.jpg?dl=0
I did this: userName: ['', [Validators.required, Validators.minLength(3)], [this.validateUserName.bind(this)]],
@HowardHo your simple comment have save my day, I just forget that we can bind this to validation.

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.