|
| 1 | +import { Component, OnInit, ElementRef } from '@angular/core'; |
| 2 | +import { ApiService } from '../api.service'; |
| 3 | +import { ValidationMessageService } from '../validation-msg.service'; |
| 4 | +import { FormBuilder, FormGroup, FormControl, Validators } from '@angular/forms'; |
| 5 | + |
| 6 | +@Component({ |
| 7 | + selector: 'app-register', |
| 8 | + templateUrl: './register.component.html', |
| 9 | + styleUrls: ['./register.component.css'] |
| 10 | +}) |
| 11 | +export class RegisterComponent implements OnInit { |
| 12 | + isLoading: Boolean = true; |
| 13 | + registerForm: FormGroup; |
| 14 | + |
| 15 | + constructor( |
| 16 | + private formBuilder: FormBuilder, |
| 17 | + private apiService: ApiService, |
| 18 | + private validErrorMsgService: ValidationMessageService, |
| 19 | + private el: ElementRef |
| 20 | + ) { } |
| 21 | + |
| 22 | + ngOnInit() { |
| 23 | + this.createForm(); |
| 24 | + this.validationErrorMsg(); |
| 25 | + } |
| 26 | + |
| 27 | + createForm() { |
| 28 | + this.registerForm = this.formBuilder.group({ |
| 29 | + firstName: ['', [Validators.required, Validators.minLength(2), Validators.maxLength(30)]], |
| 30 | + middleName: ['', [Validators.minLength(2), Validators.maxLength(30)]], |
| 31 | + lastName: ['', [Validators.required, Validators.minLength(2), Validators.maxLength(30)]], |
| 32 | + emailId: ['', [Validators.required, Validators.email]], |
| 33 | + mobile: ['', [Validators.required, Validators.pattern('^[0-9]*$'), Validators.minLength(10), , Validators.maxLength(10)]], |
| 34 | + password: ['', [Validators.required, Validators.minLength(6)]] |
| 35 | + }); |
| 36 | + } |
| 37 | + |
| 38 | + verifyForm() { |
| 39 | + const invalidElements = this.el.nativeElement.querySelectorAll('.form-control.ng-invalid'); |
| 40 | + if (invalidElements.length > 0) { |
| 41 | + invalidElements[0].focus(); |
| 42 | + } else { |
| 43 | + console.log('Registration details => ', this.registerForm.value); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /* |
| 48 | + *** Get API response as validation error json and load response in validationErrorObj of validErrorMsgService |
| 49 | + */ |
| 50 | + validationErrorMsg() { |
| 51 | + this.apiService.getValidationErrorMessage().then( |
| 52 | + (res) => { |
| 53 | + if (this.validErrorMsgService.validationErrorObj.length === 0) { |
| 54 | + this.validErrorMsgService.validationErrorObj = res['validationErrors']; |
| 55 | + console.log('Validation Error => ', this.validErrorMsgService.validationErrorObj); |
| 56 | + this.isLoading = false; |
| 57 | + } |
| 58 | + }, (error) => { |
| 59 | + console.log(error); |
| 60 | + this.isLoading = false; |
| 61 | + }); |
| 62 | +} |
| 63 | + |
| 64 | +} |
0 commit comments