From 4b2f33f3c6d109365090b08244d7f99ad4e69025 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 9 Sep 2016 22:16:51 +0200 Subject: Client: reactive forms --- client/src/app/account/account.component.html | 16 +++++----- client/src/app/account/account.component.ts | 44 +++++++++++++++++++-------- 2 files changed, 40 insertions(+), 20 deletions(-) (limited to 'client/src/app/account') diff --git a/client/src/app/account/account.component.html b/client/src/app/account/account.component.html index 4797fa914..5a8847acd 100644 --- a/client/src/app/account/account.component.html +++ b/client/src/app/account/account.component.html @@ -3,25 +3,25 @@
{{ information }}
{{ error }}
-
+
-
- The password should have more than 5 characters +
+ {{ formErrors['new-password'] }}
- + diff --git a/client/src/app/account/account.component.ts b/client/src/app/account/account.component.ts index a22738d3f..b503406c9 100644 --- a/client/src/app/account/account.component.ts +++ b/client/src/app/account/account.component.ts @@ -1,44 +1,64 @@ import { } from '@angular/common'; import { Component, OnInit } from '@angular/core'; -import { FormControl, FormGroup, Validators } from '@angular/forms'; +import { FormBuilder, FormGroup } from '@angular/forms'; import { Router } from '@angular/router'; import { AccountService } from './account.service'; +import { FormReactive, USER_PASSWORD } from '../shared'; @Component({ selector: 'my-account', template: require('./account.component.html') }) -export class AccountComponent implements OnInit { - newPassword = ''; - newConfirmedPassword = ''; - changePasswordForm: FormGroup; +export class AccountComponent extends FormReactive implements OnInit { information: string = null; error: string = null; + form: FormGroup; + formErrors = { + 'new-password': '', + 'new-confirmed-password': '' + }; + validationMessages = { + 'new-password': USER_PASSWORD.MESSAGES, + 'new-confirmed-password': USER_PASSWORD.MESSAGES + }; + constructor( private accountService: AccountService, + private formBuilder: FormBuilder, private router: Router - ) {} + ) { + super(); + } - ngOnInit() { - this.changePasswordForm = new FormGroup({ - 'new-password': new FormControl('', [ Validators.required, Validators.minLength(6) ]), - 'new-confirmed-password': new FormControl('', [ Validators.required, Validators.minLength(6) ]), + buildForm() { + this.form = this.formBuilder.group({ + 'new-password': [ '', USER_PASSWORD.VALIDATORS ], + 'new-confirmed-password': [ '', USER_PASSWORD.VALIDATORS ], }); + + this.form.valueChanges.subscribe(data => this.onValueChanged(data)); + } + + ngOnInit() { + this.buildForm(); } changePassword() { + const newPassword = this.form.value['new-password']; + const newConfirmedPassword = this.form.value['new-confirmed-password']; + this.information = null; this.error = null; - if (this.newPassword !== this.newConfirmedPassword) { + if (newPassword !== newConfirmedPassword) { this.error = 'The new password and the confirmed password do not correspond.'; return; } - this.accountService.changePassword(this.newPassword).subscribe( + this.accountService.changePassword(newPassword).subscribe( ok => this.information = 'Password updated.', err => this.error = err -- cgit v1.2.3