I have an input field which gets validated on (keypress). However this is not happening. The only time it works is if I go to another input field or click away and return. This is my code for it.
<mat-form-field>
<input matInput placeholder="Network Address" name="network" [(ngModel)]="model.network" required (keypress)="validate($event)">
<mat-hint>CIDR format, e.g. 192.168.65.0/25</mat-hint>
<mat-error *ngFor="let error of errors_by_field['network']">{{error.message}}</mat-error>
</mat-form-field>
validate() {
this.http.post<GetItemResult>('/api/branches', this.model).subscribe(data => {
this.result = data;
console.log(this.result);
this.branch = data.item;
this.errors_by_field = errors_by_field(data.errors);
for (const [field, errors] of Object.entries(this.errors_by_field)) {
if (field !== 'global') {
this.form.form.controls[field].setErrors({server_error: true});
}
}
},
err => {
this.submitting = false;
});
}
When I console log the ts code result, it prints out the result on every key press, its just not showing the on the first time the input is touched. I have to click elsewhere and return for it to work