diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-08-23 15:49:16 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-08-23 15:49:16 +0200 |
commit | 9e8aa10d94e4642ec1d20a522c41b06e9df7758b (patch) | |
tree | 6c9b7bb4a211e000d3ccad95c6eed6ff827dd373 /client/src/app/admin/friends/friend-add/friend-add.component.ts | |
parent | b5d6b94c1e192cc7b6ace0e73fc8edee2dc510ef (diff) | |
download | PeerTube-9e8aa10d94e4642ec1d20a522c41b06e9df7758b.tar.gz PeerTube-9e8aa10d94e4642ec1d20a522c41b06e9df7758b.tar.zst PeerTube-9e8aa10d94e4642ec1d20a522c41b06e9df7758b.zip |
Client: change url validation for friend add
Diffstat (limited to 'client/src/app/admin/friends/friend-add/friend-add.component.ts')
-rw-r--r-- | client/src/app/admin/friends/friend-add/friend-add.component.ts | 53 |
1 files changed, 29 insertions, 24 deletions
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 ffc499b92..16cfd8a3a 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 | |||
@@ -1,20 +1,30 @@ | |||
1 | import { Component } from '@angular/core'; | 1 | import { Component, OnInit } from '@angular/core'; |
2 | import { FormControl, FormGroup, REACTIVE_FORM_DIRECTIVES } from '@angular/forms'; | ||
2 | import { Router } from '@angular/router'; | 3 | import { Router } from '@angular/router'; |
3 | 4 | ||
5 | import { validateUrl } from '../../../shared'; | ||
4 | import { FriendService } from '../shared'; | 6 | import { FriendService } from '../shared'; |
5 | 7 | ||
6 | @Component({ | 8 | @Component({ |
7 | selector: 'my-friend-add', | 9 | selector: 'my-friend-add', |
8 | template: require('./friend-add.component.html'), | 10 | template: require('./friend-add.component.html'), |
9 | styles: [ require('./friend-add.component.scss') ] | 11 | styles: [ require('./friend-add.component.scss') ], |
12 | directives: [ REACTIVE_FORM_DIRECTIVES ] | ||
10 | }) | 13 | }) |
11 | export class FriendAddComponent { | 14 | export class FriendAddComponent implements OnInit { |
12 | urls = [ '' ]; | 15 | friendAddForm: FormGroup; |
16 | urls = [ ]; | ||
13 | error: string = null; | 17 | error: string = null; |
14 | 18 | ||
15 | constructor(private router: Router, private friendService: FriendService) {} | 19 | constructor(private router: Router, private friendService: FriendService) {} |
16 | 20 | ||
21 | ngOnInit() { | ||
22 | this.friendAddForm = new FormGroup({}); | ||
23 | this.addField(); | ||
24 | } | ||
25 | |||
17 | addField() { | 26 | addField() { |
27 | this.friendAddForm.addControl(`url-${this.urls.length}`, new FormControl('', [ validateUrl ])); | ||
18 | this.urls.push(''); | 28 | this.urls.push(''); |
19 | } | 29 | } |
20 | 30 | ||
@@ -30,6 +40,21 @@ export class FriendAddComponent { | |||
30 | return (index !== 0 || this.urls.length > 1) && index !== (this.urls.length - 1); | 40 | return (index !== 0 || this.urls.length > 1) && index !== (this.urls.length - 1); |
31 | } | 41 | } |
32 | 42 | ||
43 | isFormValid() { | ||
44 | // Do not check the last input | ||
45 | for (let i = 0; i < this.urls.length - 1; i++) { | ||
46 | if (!this.friendAddForm.controls[`url-${i}`].valid) return false; | ||
47 | } | ||
48 | |||
49 | const lastIndex = this.urls.length - 1; | ||
50 | // If the last input (which is not the first) is empty, it's ok | ||
51 | if (this.urls[lastIndex] === '' && lastIndex !== 0) { | ||
52 | return true; | ||
53 | } else { | ||
54 | return this.friendAddForm.controls[`url-${lastIndex}`].valid; | ||
55 | } | ||
56 | } | ||
57 | |||
33 | removeField(index: number) { | 58 | removeField(index: number) { |
34 | this.urls.splice(index, 1); | 59 | this.urls.splice(index, 1); |
35 | } | 60 | } |
@@ -43,11 +68,6 @@ export class FriendAddComponent { | |||
43 | return; | 68 | return; |
44 | } | 69 | } |
45 | 70 | ||
46 | if (!this.isUrlsRegexValid(notEmptyUrls)) { | ||
47 | this.error = 'Some url(s) are not valid.'; | ||
48 | return; | ||
49 | } | ||
50 | |||
51 | if (!this.isUrlsUnique(notEmptyUrls)) { | 71 | if (!this.isUrlsUnique(notEmptyUrls)) { |
52 | this.error = 'Urls need to be unique.'; | 72 | this.error = 'Urls need to be unique.'; |
53 | return; | 73 | return; |
@@ -79,21 +99,6 @@ export class FriendAddComponent { | |||
79 | return notEmptyUrls; | 99 | return notEmptyUrls; |
80 | } | 100 | } |
81 | 101 | ||
82 | // Temporary | ||
83 | // Use HTML validators | ||
84 | private isUrlsRegexValid(urls: string[]) { | ||
85 | let res = true; | ||
86 | |||
87 | const urlRegex = new RegExp('^https?://(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)$'); | ||
88 | urls.forEach((url) => { | ||
89 | if (urlRegex.test(url) === false) { | ||
90 | res = false; | ||
91 | } | ||
92 | }); | ||
93 | |||
94 | return res; | ||
95 | } | ||
96 | |||
97 | private isUrlsUnique(urls: string[]) { | 102 | private isUrlsUnique(urls: string[]) { |
98 | return urls.every(url => urls.indexOf(url) === urls.lastIndexOf(url)); | 103 | return urls.every(url => urls.indexOf(url) === urls.lastIndexOf(url)); |
99 | } | 104 | } |