From 4b2f33f3c6d109365090b08244d7f99ad4e69025 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 9 Sep 2016 22:16:51 +0200 Subject: Client: reactive forms --- .../admin/friends/friend-add/friend-add.component.html | 6 +++--- .../app/admin/friends/friend-add/friend-add.component.ts | 15 ++++++++------- 2 files changed, 11 insertions(+), 10 deletions(-) (limited to 'client/src/app/admin/friends') diff --git a/client/src/app/admin/friends/friend-add/friend-add.component.html b/client/src/app/admin/friends/friend-add/friend-add.component.html index 5b8dc8d87..788f3b44d 100644 --- a/client/src/app/admin/friends/friend-add/friend-add.component.html +++ b/client/src/app/admin/friends/friend-add/friend-add.component.html @@ -2,14 +2,14 @@
{{ error }}
-
+
@@ -17,7 +17,7 @@
-
+
It should be a valid url.
diff --git a/client/src/app/admin/friends/friend-add/friend-add.component.ts b/client/src/app/admin/friends/friend-add/friend-add.component.ts index 55aed9156..68363b482 100644 --- a/client/src/app/admin/friends/friend-add/friend-add.component.ts +++ b/client/src/app/admin/friends/friend-add/friend-add.component.ts @@ -11,19 +11,19 @@ import { FriendService } from '../shared'; styles: [ require('./friend-add.component.scss') ] }) export class FriendAddComponent implements OnInit { - friendAddForm: FormGroup; + form: FormGroup; urls = [ ]; error: string = null; constructor(private router: Router, private friendService: FriendService) {} ngOnInit() { - this.friendAddForm = new FormGroup({}); + this.form = new FormGroup({}); this.addField(); } addField() { - this.friendAddForm.addControl(`url-${this.urls.length}`, new FormControl('', [ validateUrl ])); + this.form.addControl(`url-${this.urls.length}`, new FormControl('', [ validateUrl ])); this.urls.push(''); } @@ -42,7 +42,7 @@ export class FriendAddComponent implements OnInit { isFormValid() { // Do not check the last input for (let i = 0; i < this.urls.length - 1; i++) { - if (!this.friendAddForm.controls[`url-${i}`].valid) return false; + if (!this.form.controls[`url-${i}`].valid) return false; } const lastIndex = this.urls.length - 1; @@ -50,13 +50,13 @@ export class FriendAddComponent implements OnInit { if (this.urls[lastIndex] === '' && lastIndex !== 0) { return true; } else { - return this.friendAddForm.controls[`url-${lastIndex}`].valid; + return this.form.controls[`url-${lastIndex}`].valid; } } removeField(index: number) { // Remove the last control - this.friendAddForm.removeControl(`url-${this.urls.length - 1}`); + this.form.removeControl(`url-${this.urls.length - 1}`); this.urls.splice(index, 1); } @@ -94,7 +94,8 @@ export class FriendAddComponent implements OnInit { private getNotEmptyUrls() { const notEmptyUrls = []; - this.urls.forEach((url) => { + Object.keys(this.form.value).forEach((urlKey) => { + const url = this.form.value[urlKey]; if (url !== '') notEmptyUrls.push(url); }); -- cgit v1.2.3