- Timestamp:
- 01/24/22 21:08:32 (2 years ago)
- Branches:
- master
- Children:
- 76712b2
- Parents:
- bdd6491
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trip-planner-front/src/app/homepage/register/register.component.ts
rbdd6491 rb738035 1 1 import { Component, OnInit } from '@angular/core'; 2 import { FormBuilder, Form Group, Validators } from '@angular/forms';2 import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms'; 3 3 import { DynamicDialogRef } from 'primeng/dynamicdialog'; 4 import { CustomValidators } from 'src/app/providers/CustomValidators'; 4 5 import { UserDto } from 'src/app/_models/dto/userDto'; 6 import { UserService } from 'src/app/_services/user.service'; 5 7 6 8 @Component({ … … 12 14 13 15 user: UserDto; 14 myForm: FormGroup; 16 form: FormGroup; 17 usernames: string[]; 15 18 16 constructor(private ref: DynamicDialogRef, private fb: FormBuilder) { 19 20 constructor(private ref: DynamicDialogRef, 21 formBuilder: FormBuilder, private userService: UserService) { 17 22 this.user = new UserDto(); 18 this.myForm = fb.group({ 19 title: fb.control('initial value', Validators.required) 23 this.usernames = []; 24 25 this.form = formBuilder.group({ 26 fullName: new FormControl('', [Validators.required, Validators.minLength(3)]), 27 username: new FormControl('', [Validators.required, Validators.email]), 28 password: new FormControl('', [Validators.required, Validators.minLength(6)]), 29 confirmPassword: new FormControl('', [Validators.required, Validators.minLength(6)]) 30 }, { 31 validators: [ 32 CustomValidators.mustMatch('password', 'confirmPassword'), 33 this.validateUsername('username') 34 ] 20 35 }); 21 36 } 22 37 23 38 ngOnInit(): void { 24 this.myForm = this.fb.group({25 fullName: this.fb.control('', Validators.required),26 username: this.fb.control('', Validators.required),27 password: this.fb.control('', Validators.required),28 confirmPassword: this.fb.control('', Validators.required)29 });30 39 } 31 40 32 41 onFormSubmitSignUp(user) { 33 this.ref.close(user); 34 //window.location.reload(); 42 const { valid } = this.form; 43 if (valid) { 44 this.ref.close(user); 45 } 46 } 47 48 validateUsername(username: string) { 49 return (formGroup: FormGroup) => { 50 const control = formGroup.controls[username]; 51 52 if (control.errors && !control.errors.validateUsername) { 53 return; 54 } 55 56 this.userService.getAllUsernames().subscribe( 57 data => { 58 this.usernames = data; 59 for (let i = 0; i < this.usernames.length; i++) { 60 if (control.value === this.usernames[i]) { 61 control.setErrors({ validateUsername: true }); 62 break; 63 } else { 64 control.setErrors(null); 65 } 66 } 67 } 68 ); 69 return null; 70 }; 35 71 36 72 }
Note:
See TracChangeset
for help on using the changeset viewer.