From 4948ab9ee51ef1cd9c91157591496995447fba6d Mon Sep 17 00:00:00 2001 From: Tim Bowersox <11789189+t-bowersox@users.noreply.github.com> Date: Fri, 21 Jan 2022 19:17:41 -0500 Subject: [PATCH] docs: expand async validators in reactive forms (#44764) * Hone docregions for code examples * Add more detailed description PR Close #44764 --- .../hero-form-reactive.component.2.ts | 19 ++++++++++++------- aio/content/guide/form-validation.md | 10 ++++++++-- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/aio/content/examples/form-validation/src/app/reactive/hero-form-reactive.component.2.ts b/aio/content/examples/form-validation/src/app/reactive/hero-form-reactive.component.2.ts index 871da936e17..d01375672b3 100644 --- a/aio/content/examples/form-validation/src/app/reactive/hero-form-reactive.component.2.ts +++ b/aio/content/examples/form-validation/src/app/reactive/hero-form-reactive.component.2.ts @@ -19,18 +19,21 @@ export class HeroFormReactiveComponent implements OnInit { heroForm: FormGroup; ngOnInit(): void { + // #docregion async-validator-usage + const alterEgoControl = new FormControl('', { + asyncValidators: [this.alterEgoValidator.validate.bind(this.alterEgoValidator)], + updateOn: 'blur' + }); + alterEgoControl.setValue(this.hero.alterEgo); + // #enddocregion async-validator-usage + this.heroForm = new FormGroup({ name: new FormControl(this.hero.name, [ Validators.required, Validators.minLength(4), forbiddenNameValidator(/bob/i) ]), - // #docregion async-validator - alterEgo: new FormControl(this.hero.alterEgo, { - asyncValidators: [this.alterEgoValidator.validate.bind(this.alterEgoValidator)], - updateOn: 'blur' - }), - // #enddocregion async-validator + alterEgo: alterEgoControl, power: new FormControl(this.hero.power, Validators.required) }); } @@ -40,6 +43,8 @@ export class HeroFormReactiveComponent implements OnInit { get power() { return this.heroForm.get('power'); } get alterEgo() { return this.heroForm.get('alterEgo'); } - + + // #docregion async-validator-inject constructor(private alterEgoValidator: UniqueAlterEgoValidator) {} + // #enddocregion async-validator-inject } diff --git a/aio/content/guide/form-validation.md b/aio/content/guide/form-validation.md index c969d1b9b9c..50a6bcef8fb 100644 --- a/aio/content/guide/form-validation.md +++ b/aio/content/guide/form-validation.md @@ -335,9 +335,15 @@ The `pending` flag is set to `false`, and the form validity is updated. ### Adding async validators to reactive forms -In reactive forms, add an async validator by passing the function directly to the `FormControl`. +To use an async validator in reactive forms, begin by injecting the validator into the constructor of the component class. - + + +Then, pass the validator function directly to the `FormControl` to apply it. + +In the following example, the `validate` function of `UniqueAlterEgoValidator` is applied to `alterEgoControl` by passing it to the control's `asyncValidators` option and binding it to the instance of `UniqueAlterEgoValidator` that was injected into `HeroFormReactiveComponent`. The value of `asyncValidators` can be either a single async validator function, or an array of functions. + + ### Adding async validators to template-driven forms