Is there anyway to Call method automatically like checkValidity() to set { 'invalid': false/true } after async form validation (i.e i have this.uniqueNameValidator.bind(this)) async call)
Checked https://stackoverflow.com/a/49520534/1225526 but it looks like submit method call after submit click action
Tried Form.valueChanges not work as expected it is called before validation
this.editForm.valueChanges
.pipe(takeUntil(this._onDestroy))
.subscribe(changes => {
if (changes) {
this.onChange(Object.assign({userId: this.userId}, changes));
}
this.checkValidity(); <-- This part is not working as expected it is called before validation
});
checkValidity() {
if (((this.editForm.controls.userName.pristine && this.editForm.controls.userName.pending) ||
!this.editForm.controls.userName.invalid)
&& !this.editForm.controls.password.invalid) {
this.editForm.setErrors({ 'invalid': false });
} else {
this.editForm.setErrors({ 'invalid': true });
}
}
this.editForm = new FormGroup({
userDisplayName: new FormControl({ value: '', disabled: true }),
userName: new FormControl('', [ Validators.required],
this.uniqueNameValidator.bind(this)), <-- async Validation
password: new FormControl('',[Validators.required,
validateUserPassword()])});
uniqueNameValidator(control: AbstractControl) {
return control.valueChanges.pipe(
switchMap(() => this.service.checkUsernameAvailability(control.value)),
take(1),
map(({data}) => {
control.markAsTouched();
this.componentChangeDetector.markForCheck();
return data.checkUsernameAvailability ? null : {invalid: false};
}), catchError((error): any => {
return of(null);
}));
}
Any help would be great.
AbstractControl provides two Observables
valueChanges
and statusChanges
, maybe those can help you.