import { Component, OnInit, Output, EventEmitter, Input } from '@angular/core'; import {FormGroup, FormBuilder, Validators, FormControl, ValidationErrors, AbstractControl} from '@angular/forms'; import {Member} from '@overflow/commons-typescript/model/member'; @Component({ selector: 'of-member-modify-password', templateUrl: './member-modify-password.component.html', }) export class MemberModifyPasswordComponent implements OnInit { @Input() token: string; @Input() member: Member; @Output() modifyPassword = new EventEmitter<{token: string, password: string, confirmPassword: string}>(); @Output() signin = new EventEmitter(); modifyPasswordForm: FormGroup; password: AbstractControl; pwConfirm: AbstractControl; constructor( private formBuilder: FormBuilder, ) { } ngOnInit() { this.initForm(); } initForm() { this.modifyPasswordForm = this.formBuilder.group({ 'password': [ '', [ Validators.required, Validators.pattern('^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{6,}$'), Validators.minLength(6), Validators.maxLength(25), ] ], 'pwConfirm': [ '', [ Validators.compose([ Validators.required, this.pwMatchValidator ]) ] ] }); this.password = this.modifyPasswordForm.controls['password']; this.pwConfirm = this.modifyPasswordForm.controls['pwConfirm']; } pwMatchValidator(control: FormControl): ValidationErrors { let pw; if (control.parent) { pw = control.parent.controls['password'].value; } if (control.value !== pw) { return { notMatched: true }; } return null; } modifyPasswordFormSubmit() { const formValue = Object.assign({}, this.modifyPasswordForm.value); this.modifyPassword.emit({token: this.token, password: formValue.pw, confirmPassword: formValue.confirmPw}); } onSignin() { this.signin.emit(); } }