Skip to content

Instantly share code, notes, and snippets.

@aimanrahmattt
Created November 27, 2019 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aimanrahmattt/b4b5df9da5c95d857e1ee684582bd1ae to your computer and use it in GitHub Desktop.
Save aimanrahmattt/b4b5df9da5c95d857e1ee684582bd1ae to your computer and use it in GitHub Desktop.
app.component.ts
import { Component, OnInit } from '@angular/core';
import { faCar } from '@fortawesome/free-solid-svg-icons';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
signUpForm: FormGroup;
submitted: boolean = false;
constructor(private fb: FormBuilder) {
}
ngOnInit() {
this.signUpForm = this.fb.group({
firstname: ['', Validators.required],
lastname: ['', Validators.required],
username: ['', Validators.required],
email: ['', [Validators.required, Validators.pattern('[a-zA-Z0-9.-]{1,}@[a-zA-Z.-]{2,}[.]{1}[a-zA-Z]{3,}')]],
password: ['', [Validators.required, Validators.minLength(6)]],
confirmPassword: ['', Validators.required]
}, {
validator: this.mustMatch('password', 'confirmPassword')
});
}
get f() { return this.signUpForm.controls; }
onSubmit() {
this.submitted = true;
if (this.signUpForm.invalid) {
this.scrollToError();
}else {
console.log("Complete");
}
}
scrollTo(el: Element): void {
if (el) {
el.scrollIntoView({ behavior: 'smooth', block: 'center' });
}
}
scrollToError(): void {
const firstElementWithError = document.querySelector('.ng-invalid[formControlName]');
console.log("aa:", firstElementWithError);
this.scrollTo(firstElementWithError);
}
mustMatch(controlName: string, matchingControlName: string) {
return (formGroup: FormGroup) => {
const control = formGroup.controls[controlName];
const matchingControl = formGroup.controls[matchingControlName];
if (matchingControl.errors && !matchingControl.errors.mustMatch) {
// return if another validator has already found an error on the matchingControl
return;
}
// set error on matchingControl if validation fails
if (control.value !== matchingControl.value) {
matchingControl.setErrors({ mustMatch: true });
} else {
matchingControl.setErrors(null);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment