Reputation: 3665
I have an an async validator on a zip
field:
zip: ['', {
validators: [
Validators.required,
Validators.minLength(5),
Validators.maxLength(5)
],
asyncValidators: [
adPostFormValidators.isValidZip(this.locationService)
]
},
],
However, the field doesn't seem to reflect the error that comes from the async validator until I click out of the field. For example, this is before I click out (the null
is the field's error state):
I know that the async validator has run because I output its results into the console:
Then, when I click out or lose focus, the error state is now accurate:
However, the validator was not run again, as nothing new was logged into the console.
Upvotes: 2
Views: 1027
Reputation: 3665
So it seems that because I'm using ChangeDetectionStrategy.OnPush
, I had to manually mark async validators for check:
ref.markForCheck()
return { 'Is not a valid zip': { value: control.value } } as ValidationErrors
where ref
is of type ChangeDetectorRef
.
Upvotes: 6