From: Kai Moritz Date: Sun, 25 Dec 2022 21:53:18 +0000 (+0100) Subject: feat: A Username must not be empty X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=dd18c97daff609633700d4ba360d2f6f4440b7d6;p=demos%2Fkafka%2Fchat feat: A Username must not be empty --- diff --git a/src/app/user/user.component.html b/src/app/user/user.component.html index b640cfd6..3cfc8d3f 100644 --- a/src/app/user/user.component.html +++ b/src/app/user/user.component.html @@ -14,6 +14,7 @@
Name is required.
+
The username must not be empty
diff --git a/src/app/user/user.component.ts b/src/app/user/user.component.ts index 0ec30268..23817da3 100644 --- a/src/app/user/user.component.ts +++ b/src/app/user/user.component.ts @@ -11,16 +11,22 @@ import { UserService } from "../user.service"; }) export class UserComponent { - usernameForm = new FormControl('', Validators.required); + usernameForm = new FormControl('', [ Validators.required, this.noWhitespaceValidator ]); updateName(): void { var input = this.usernameForm.getRawValue(); if (input !== null) { - this.userService.setUser(input); + this.userService.setUser(input.trim()); this.router.navigate(['chatrooms']) } } + noWhitespaceValidator(control: FormControl) { + const isWhitespace = (control.value || '').trim().length === 0; + const isValid = !isWhitespace; + return isValid ? null : { 'whitespace': true }; + } + constructor( private userService: UserService, private router: Router) {}