Client: change url validation for friend add
authorChocobozzz <florian.bigard@gmail.com>
Tue, 23 Aug 2016 13:49:16 +0000 (15:49 +0200)
committerChocobozzz <florian.bigard@gmail.com>
Tue, 23 Aug 2016 13:49:16 +0000 (15:49 +0200)
client/src/app/admin/friends/friend-add/friend-add.component.html
client/src/app/admin/friends/friend-add/friend-add.component.ts
client/src/app/shared/form-validators/index.ts [new file with mode: 0644]
client/src/app/shared/form-validators/url.validator.ts [new file with mode: 0644]
client/src/app/shared/index.ts
client/tsconfig.json

index d8bb740b4677d5d58e0e9e53e8dd72634270deed..5b8dc8d87a04bdf7de9b7024577b857c152ba241 100644 (file)
@@ -2,17 +2,25 @@
 
 <div *ngIf="error" class="alert alert-danger">{{ error }}</div>
 
-<form (ngSubmit)="makeFriends()">
+<form (ngSubmit)="makeFriends()" [formGroup]="friendAddForm">
   <div class="form-group"  *ngFor="let url of urls; let id = index; trackBy:customTrackBy">
     <label for="username">Url</label>
+
     <div class="input-group">
-      <input type="text" class="form-control" [name]="'url-' + id"  [id]="'url-' + id" placeholder="http://domain.com" [(ngModel)]="urls[id]" />
+      <input
+        type="text" class="form-control" placeholder="http://domain.com"
+        [name]="'url-' + id"  [id]="'url-' + id" [formControlName]="'url-' + id" [(ngModel)]="urls[id]"
+      />
       <span class="input-group-btn">
         <button *ngIf="displayAddField(id)" (click)="addField()" class="btn btn-default" type="button">+</button>
-        <button *ngIf="displayRemoveField(id)" (click)="removeField(index)" class="btn btn-default" type="button">-</button>
+        <button *ngIf="displayRemoveField(id)" (click)="removeField(id)" class="btn btn-default" type="button">-</button>
       </span>
     </div>
+
+    <div [hidden]="friendAddForm.controls['url-' + id].valid || friendAddForm.controls['url-' + id].pristine" class="alert alert-warning">
+      It should be a valid url.
+    </div>
   </div>
 
-  <input type="submit" value="Make friends" class="btn btn-default">
+  <input type="submit" value="Make friends" class="btn btn-default" [disabled]="!isFormValid()">
 </form>
index ffc499b92bb65ccc2ecd9c90fa945474e886934f..16cfd8a3ab959f64ed247a2f78609d228c40e9d9 100644 (file)
@@ -1,20 +1,30 @@
-import { Component } from '@angular/core';
+import { Component, OnInit } from '@angular/core';
+import { FormControl, FormGroup, REACTIVE_FORM_DIRECTIVES } from '@angular/forms';
 import { Router } from '@angular/router';
 
+import { validateUrl } from '../../../shared';
 import { FriendService } from '../shared';
 
 @Component({
   selector: 'my-friend-add',
   template: require('./friend-add.component.html'),
-  styles: [ require('./friend-add.component.scss') ]
+  styles: [ require('./friend-add.component.scss') ],
+  directives: [ REACTIVE_FORM_DIRECTIVES ]
 })
-export class FriendAddComponent {
-  urls = [ '' ];
+export class FriendAddComponent implements OnInit {
+  friendAddForm: FormGroup;
+  urls = [ ];
   error: string = null;
 
   constructor(private router: Router, private friendService: FriendService) {}
 
+  ngOnInit() {
+    this.friendAddForm = new FormGroup({});
+    this.addField();
+  }
+
   addField() {
+    this.friendAddForm.addControl(`url-${this.urls.length}`, new FormControl('', [ validateUrl ]));
     this.urls.push('');
   }
 
@@ -30,6 +40,21 @@ export class FriendAddComponent {
     return (index !== 0 || this.urls.length > 1) && index !== (this.urls.length - 1);
   }
 
+  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;
+    }
+
+    const lastIndex = this.urls.length - 1;
+    // If the last input (which is not the first) is empty, it's ok
+    if (this.urls[lastIndex] === '' && lastIndex !== 0) {
+      return true;
+    } else {
+      return this.friendAddForm.controls[`url-${lastIndex}`].valid;
+    }
+  }
+
   removeField(index: number) {
     this.urls.splice(index, 1);
   }
@@ -43,11 +68,6 @@ export class FriendAddComponent {
       return;
     }
 
-    if (!this.isUrlsRegexValid(notEmptyUrls)) {
-      this.error = 'Some url(s) are not valid.';
-      return;
-    }
-
     if (!this.isUrlsUnique(notEmptyUrls)) {
       this.error = 'Urls need to be unique.';
       return;
@@ -79,21 +99,6 @@ export class FriendAddComponent {
     return notEmptyUrls;
   }
 
-  // Temporary
-  // Use HTML validators
-  private isUrlsRegexValid(urls: string[]) {
-    let res = true;
-
-    const urlRegex = new RegExp('^https?://(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)$');
-    urls.forEach((url) => {
-      if (urlRegex.test(url) === false) {
-        res = false;
-      }
-    });
-
-    return res;
-  }
-
   private isUrlsUnique(urls: string[]) {
     return urls.every(url => urls.indexOf(url) === urls.lastIndexOf(url));
   }
diff --git a/client/src/app/shared/form-validators/index.ts b/client/src/app/shared/form-validators/index.ts
new file mode 100644 (file)
index 0000000..f9e9a61
--- /dev/null
@@ -0,0 +1 @@
+export * from './url.validator';
diff --git a/client/src/app/shared/form-validators/url.validator.ts b/client/src/app/shared/form-validators/url.validator.ts
new file mode 100644 (file)
index 0000000..67163b4
--- /dev/null
@@ -0,0 +1,11 @@
+import { FormControl } from '@angular/forms';
+
+export function validateUrl(c: FormControl) {
+  let URL_REGEXP = new RegExp('^https?://(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)$');
+
+  return URL_REGEXP.test(c.value) ? null : {
+    validateUrl: {
+      valid: false
+    }
+  };
+}
index c05e8d2539edfd0838ad28e8264284a9b1318824..9edf9b4a0a713791446181ab948ecd312ce09a75 100644 (file)
@@ -1,3 +1,4 @@
 export * from './auth';
+export * from './form-validators';
 export * from './search';
 export * from './users';
index 87a06b0c68edd0d62fbf51feb233ce960fd67337..53e6fd5716c3380b6d37d370a5d555e83d0d6739 100644 (file)
@@ -65,6 +65,8 @@
     "src/app/shared/auth/auth-user.model.ts",
     "src/app/shared/auth/auth.service.ts",
     "src/app/shared/auth/index.ts",
+    "src/app/shared/form-validators/index.ts",
+    "src/app/shared/form-validators/url.validator.ts",
     "src/app/shared/index.ts",
     "src/app/shared/search/index.ts",
     "src/app/shared/search/search-field.type.ts",