Skip to content
This repository was archived by the owner on Jan 5, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ export class FormValidationDirective {
} else if (control instanceof FormControl && control.enabled) {
control.markAsDirty();
control.markAsTouched();
control.updateValueAndValidity();
if (!control.asyncValidator) {
// no reason to recheck async validators
control.updateValueAndValidity();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
<label>Max Value</label>
<input type="number" class="form-control" placeholder="Required Field" formControlName="maxValue">
</div>
<div class="form-group">
<label>Async Validator</label>
<input type="number" class="form-control" placeholder="Required Field" formControlName="async">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
<button type="button" class="btn btn-danger" (click)="this.handleReset()">Reset</button>
</form>
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { Component, OnInit } from "@angular/core";
import { FormGroup, Validators, FormControl } from "@angular/forms";
import {
FormGroup,
Validators,
FormControl,
AbstractControl
} from "@angular/forms";
import { of } from "rxjs";
import { delay, map } from "rxjs/operators";

@Component({
selector: "app-default-errors-demo",
Expand All @@ -12,7 +19,8 @@ export class DefaultErrorsDemoComponent implements OnInit {
requiredField: new FormControl("", Validators.required),
pattern: new FormControl("", Validators.pattern(/foobar/)),
minValue: new FormControl(0, Validators.min(10)),
maxValue: new FormControl(10, Validators.max(5))
maxValue: new FormControl(10, Validators.max(5)),
async: new FormControl(10, null, this.asyncValidator.bind(this))
});

constructor() {}
Expand All @@ -29,4 +37,12 @@ export class DefaultErrorsDemoComponent implements OnInit {
maxValue: 10
});
}

asyncValidator(control: AbstractControl) {
const valid = control.value > 10;
return of(valid).pipe(
delay(500),
map(result => (result ? null : { asyncValidation: true }))
);
}
}