import { Component } from '@angular/core';
import { FormControl } from '@angular/forms';
+import { Validators } from '@angular/forms';
import { Router } from "@angular/router";
import { UserService } from "../user.service";
})
export class UserComponent {
- name = new FormControl('');
+ usernameForm = new FormControl('', [ Validators.required, this.noWhitespaceValidator ]);
updateName(): void {
- var input = this.name.getRawValue();
+ 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) {}