Skip to content

Commit 12d95dc

Browse files
authored
Merge pull request #145 from doggy8088/patch-2
Add ng-async-validator snippet
2 parents 689378a + 7a26f70 commit 12d95dc

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

src/snippets.json

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,33 @@
272272
],
273273
"description": "Angular Validator Function"
274274
},
275+
"Angular Async Validator": {
276+
"prefix": "ng-async-validator",
277+
"description": "Angular async validator snippet",
278+
"types": "typescript",
279+
"body": [
280+
"import { Directive } from '@angular/core';",
281+
"import { NG_ASYNC_VALIDATORS, AbstractControl, AsyncValidator, ValidationErrors } from '@angular/forms';",
282+
"import { Observable, of } from 'rxjs';",
283+
"",
284+
"@Directive({",
285+
"\tselector: '[${validatorName}][ngModel]',",
286+
"\tproviders: [",
287+
"\t\t{ provide: NG_ASYNC_VALIDATORS, useExisting: ${className}, multi: true }",
288+
"\t]",
289+
"})",
290+
"",
291+
"export class ${className} implements AsyncValidator {",
292+
"\tvalidate(c: AbstractControl): Promise<ValidationErrors | null> | Observable<ValidationErrors | null> {",
293+
"\t\tif (!c.value) return of(null);",
294+
"",
295+
"\t\treturn of({",
296+
"\t\t\t'${validatorName}': true",
297+
"\t\t});",
298+
"\t}",
299+
"}"
300+
]
301+
},
275302
"Angular Async Validator Function": {
276303
"prefix": "ng-async-validator-fn",
277304
"body": [
@@ -439,7 +466,7 @@
439466
"import { MatTabsModule } from '@angular/material/tabs';",
440467
"import { MatToolbarModule } from '@angular/material/toolbar';",
441468
"import { MatTreeModule } from '@angular/material/tree';",
442-
469+
443470
"const materialModules = [",
444471
"\t\tA11yModule",
445472
"\t\tBidiModule",
@@ -476,7 +503,7 @@
476503
"\t\tMatButtonToggleModule,",
477504
"\t\tMatTreeModule,",
478505
"\t\t];",
479-
506+
480507
"@NgModule({",
481508
"imports: [...materialModules],",
482509
"exports: [...materialModules],",

0 commit comments

Comments
 (0)