Created
August 18, 2020 06:17
-
-
Save onwuvic/e630fad6fa884cff3447a4f16883f911 to your computer and use it in GitHub Desktop.
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 { Component, OnInit, ViewChildren, ElementRef, AfterViewInit } from '@angular/core'; | |
import { FormGroup, FormBuilder, Validators, FormControlName } from '@angular/forms'; | |
import { Observable, fromEvent, merge } from 'rxjs'; | |
import { debounceTime } from 'rxjs/operators'; | |
import { GenericValidator } from '../../shared/generic-validator'; | |
@Component({ | |
selector: 'app-login', | |
templateUrl: './login.component.html', | |
styleUrls: ['./login.component.scss'] | |
}) | |
export class LoginComponent implements OnInit, AfterViewInit { | |
// Access every form input fields in our login html file | |
@ViewChildren(FormControlName, { read: ElementRef }) formInputElements: ElementRef[]; | |
loginForm: FormGroup; | |
// Use with the generic validation message class | |
displayMessage: { [key: string]: string } = {}; | |
private validationMessages: { [key: string]: { [key: string]: string } }; | |
private genericValidator: GenericValidator; | |
constructor(private fb: FormBuilder) { | |
// Defines all of the validation messages for the form. | |
this.validationMessages = { | |
email: { | |
required: 'Required', | |
email: 'This email is invalid' | |
}, | |
password: { | |
required: 'Required', | |
minlength: 'The password length must be greater than or equal to 8' | |
} | |
}; | |
// Define an instance of the validator for use with this form, | |
// passing in this form's set of validation messages. | |
this.genericValidator = new GenericValidator(this.validationMessages); | |
} | |
ngOnInit() { | |
this.loginForm = this.fb.group({ | |
email: ['', [Validators.required, Validators.email]], | |
password: ['', [Validators.required, Validators.minLength(8)]], | |
}); | |
} | |
ngAfterViewInit(): void { | |
// Watch for the blur event from any input element on the form. | |
const controlBlurs: Observable<any>[] = this.formInputElements | |
.map((formControl: ElementRef) => fromEvent(formControl.nativeElement, 'blur')); | |
// Merge the blur event observable with the valueChanges observable | |
merge(this.loginForm.valueChanges, ...controlBlurs).pipe( | |
debounceTime(800) | |
).subscribe(value => { | |
this.displayMessage = this.genericValidator.processMessages(this.loginForm); | |
}); | |
} | |
login() { | |
console.log('---form', this.loginForm.value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment