Skip to content

Instantly share code, notes, and snippets.

@onwuvic
Created August 18, 2020 05:55
Show Gist options
  • Save onwuvic/e993c9072810fc186953185922d31ab3 to your computer and use it in GitHub Desktop.
Save onwuvic/e993c9072810fc186953185922d31ab3 to your computer and use it in GitHub Desktop.
import { Component, OnInit, ViewChildren, ElementRef, AfterViewInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators, FormControlName, AbstractControl } from '@angular/forms';
import { Observable, fromEvent, merge } from 'rxjs';
import { debounceTime } from 'rxjs/operators';
import { GenericValidator } from '../../shared/generic-validator';
import { PasswordMatcher } from '../../shared/password-matcher';
@Component({
selector: 'app-sign-up',
templateUrl: './sign-up.component.html',
styleUrls: ['./sign-up.component.scss']
})
export class SignUpComponent implements OnInit, AfterViewInit {
// Access every form input fields in our signup html file
@ViewChildren(FormControlName, { read: ElementRef }) formInputElements: ElementRef[];
signupForm: FormGroup;
// Use with the generic validation message class
displayMessage: { [key: string]: string } = {};
private genericValidator: GenericValidator;
constructor(private fb: FormBuilder) {
// Define an instance of the validator for use with this form,
this.genericValidator = new GenericValidator();
}
ngOnInit() {
this.signupForm = this.fb.group({
firstName: ['', [Validators.required]],
lastName: ['', [Validators.required]],
email: ['', [Validators.required, Validators.email]],
password: ['', [Validators.required, Validators.minLength(8)]],
confirmPassword: ['', Validators.required]
}, { validator: PasswordMatcher.match });
}
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.signupForm.valueChanges, ...controlBlurs).pipe(
debounceTime(800)
).subscribe(value => {
this.displayMessage = this.genericValidator.processMessages(this.signupForm);
});
}
signup() {
console.log('---form', this.signupForm.value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment