Last active
August 2, 2018 08:17
-
-
Save hbweb/5c13502f57d465b220141a8e1e9e218d to your computer and use it in GitHub Desktop.
Angular Async Validator - Medium Post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Directive } from '@angular/core'; | |
import { AsyncValidatorFn, AsyncValidator, NG_ASYNC_VALIDATORS, AbstractControl, ValidationErrors } from '@angular/forms'; | |
import { Observable } from "rxjs/Observable"; | |
import 'rxjs/add/operator/map'; | |
import { ProductService } from '../product-service'; | |
import { map, tap } from 'rxjs/operators'; | |
export function existingProductCodeValidator(productService: ProductService): AsyncValidatorFn { | |
return (control: AbstractControl): Promise<ValidationErrors | null> | Observable<ValidationErrors | null> => { | |
return productService.getProductByCode(control.value).pipe( | |
map(product => product ? {"productExists": true} : null), | |
tap(res => control.setErrors(res)) | |
).first(); | |
}; | |
} | |
@Directive({ | |
selector: '[productExists][formControlName],[productExists][formControl],[productExists][ngModel]', | |
providers: [{provide: NG_ASYNC_VALIDATORS, useExisting: ExistingProductValidatorDirective, multi: true}] | |
}) | |
export class ExistingProductValidatorDirective implements AsyncValidator { | |
constructor(private userService: UserService) { } | |
validate(control: AbstractControl): Promise<ValidationErrors | null> | Observable<ValidationErrors | null> { | |
return existingProductCodeValidator(this.productService)(control); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="form-group"> | |
<label for="productCode">Enter Product Code: </label> | |
<input formControlName="productCode" id="productCode" /> | |
<div *ngIf="form.get('productCode').hasError('productExists')"> | |
Product code found! Please continue. | |
</div> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { existingProductCodeValidator } from 'email-validator.directive'; // Import product code validator function | |
@Component({ | |
selector: 'product-search', | |
templateUrl: './product-search.component.html' | |
}) | |
export class ProductSearchFormComponent implements OnInit { | |
userForm: FormGroup; | |
constructor(private formBuilder:FormBuilder, private productService: ProductService) { | |
} | |
ngOnInit() { | |
this.userForm = this.formBuilder.group({ | |
productCode: ['', | |
[ Validators.required ], //sync validators | |
[ existingProductCodeValidator(this.productService) ] //async validators | |
], | |
otherFields: ['', | |
[ Validators.required], //sync validators | |
] | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment