From b9d3ea542c990f10b2c71894a1c17e777eba2bbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Mau=C3=9F?= <lukas.mauss@mni.thm.de> Date: Tue, 5 Mar 2019 13:27:01 +0100 Subject: [PATCH] Fix Validation for input field --- .../shared/room-join/room-join.component.html | 2 +- .../shared/room-join/room-join.component.ts | 15 +++++++++++++-- src/assets/i18n/home/de.json | 3 ++- src/assets/i18n/home/en.json | 3 ++- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/app/components/shared/room-join/room-join.component.html b/src/app/components/shared/room-join/room-join.component.html index 0df7178cf..0d3297e0e 100644 --- a/src/app/components/shared/room-join/room-join.component.html +++ b/src/app/components/shared/room-join/room-join.component.html @@ -8,7 +8,7 @@ placeholder="Session-Id" [formControl]="roomFormControl" [errorStateMatcher]="matcher"/> <mat-hint align="end">{{ roomId.value.length - (roomId.value.split(' ').length -1) }} / 8</mat-hint> <mat-error *ngIf="roomFormControl.hasError('required')">{{ 'home-page.please-enter' | translate}}</mat-error> - <mat-error *ngIf="roomFormControl.hasError('minlength')">{{ 'home-page.exactly-8' | translate}}</mat-error> + <mat-error *ngIf="roomFormControl.hasError('pattern')">{{ 'home-page.only-numbers' | translate}}</mat-error> </mat-form-field> <button mat-fab color="accent" type="submit"> <mat-icon>input</mat-icon> diff --git a/src/app/components/shared/room-join/room-join.component.ts b/src/app/components/shared/room-join/room-join.component.ts index 91fe23420..ce606ce85 100644 --- a/src/app/components/shared/room-join/room-join.component.ts +++ b/src/app/components/shared/room-join/room-join.component.ts @@ -30,7 +30,7 @@ export class RoomJoinComponent implements OnInit { demoId = '95680586'; user: User; - roomFormControl = new FormControl('', [Validators.required, Validators.minLength(9), Validators.maxLength(9)]); + roomFormControl = new FormControl('', [Validators.required, Validators.pattern('[0-9 ]*')]); matcher = new RegisterErrorStateMatcher(); @@ -46,7 +46,16 @@ export class RoomJoinComponent implements OnInit { } getRoom(id: string): void { - this.roomService.getRoomByShortId(id.replace(/\s/g, "")) + if (id.length -1 < 8) { + this.translateService.get('home-page.exactly-8').subscribe(message => { + this.notificationService.show(message); + }); + } else if (this.roomFormControl.hasError('pattern')) { + this.translateService.get('home-page.only-numbers').subscribe(message => { + this.notificationService.show(message); + }); + } else { + this.roomService.getRoomByShortId(id.replace(/\s/g, "")) .subscribe(room => { this.room = room; if (!room) { @@ -65,6 +74,8 @@ export class RoomJoinComponent implements OnInit { } } }); + } + } joinRoom(id: string): void { diff --git a/src/assets/i18n/home/de.json b/src/assets/i18n/home/de.json index 422bf9395..567c75242 100644 --- a/src/assets/i18n/home/de.json +++ b/src/assets/i18n/home/de.json @@ -25,7 +25,8 @@ "create-session": "Session erstellen", "no-empty-name": "Bitte geben Sie einen Namen ein.", "created-1": "Session '", - "created-2": "' erfolgreich erstellt." + "created-2": "' erfolgreich erstellt.", + "only-numbers": "Eine Session-Id besteht aus Nummern." }, "login": { "email": "E-Mail", diff --git a/src/assets/i18n/home/en.json b/src/assets/i18n/home/en.json index ad19e53b9..b3853c79b 100644 --- a/src/assets/i18n/home/en.json +++ b/src/assets/i18n/home/en.json @@ -25,7 +25,8 @@ "create-session": "Create session", "no-empty-name": "Please enter a name.", "created-1": "Session '", - "created-2": "' successfully created." + "created-2": "' successfully created.", + "only-numbers": "A session-id only contains numbers." }, "login": { "email": "E-Mail", -- GitLab