Reputation: 11888
I have done a custom validator which I apply on one of my two input:
<input type="text"
class="validate"
[(ngModel)]="foo"
name="foo"
ngControl="foo"
customValidator
[validatorArg]="blah"
/>
<input type="text"
class="validate"
[(ngModel)]="blah"
name="blah"
/>
The customValidator
depends on the second input. So what I'd like here, would be to trigger the validator on the foo
input when I do modified the blah
input.
Any idea how to do that ? I could do it on ngModelChange
EDIT: My inputs are part of a form which is a FormGroup
Upvotes: 1
Views: 1715
Reputation: 8421
It's a bit problematic with template driven forms. I have done it like this
<input name="blah" ... (ngModelChange)="triggerValidation(formObject.controls.foo)">
triggerValidation(control: AbstractControl) {
control.markAsTouched();
setTimeout(() => control.updateValueAndValidity(), 0);
}
If you run updateValueAndValidity() synchronously (without timeout), it may use an old value of the "blah" input.
Upvotes: 7