From ea7da8dc7d52bcaa71349b6dcc4e232206af5da3 Mon Sep 17 00:00:00 2001 From: Piotr Belke Date: Mon, 30 Sep 2024 20:09:21 +0200 Subject: [PATCH] IKC-414 Cluster name validation --- .../src/lib/cluster-form/cluster-form.component.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/kouncil-frontend/libs/feat-clusters/src/lib/cluster-form/cluster-form.component.ts b/kouncil-frontend/libs/feat-clusters/src/lib/cluster-form/cluster-form.component.ts index c46c3f5c..43dfb701 100644 --- a/kouncil-frontend/libs/feat-clusters/src/lib/cluster-form/cluster-form.component.ts +++ b/kouncil-frontend/libs/feat-clusters/src/lib/cluster-form/cluster-form.component.ts @@ -6,6 +6,7 @@ import { FormControl, FormGroup, ValidationErrors, + ValidatorFn, Validators } from '@angular/forms'; import {ClusterService} from './cluster.service'; @@ -99,7 +100,7 @@ export class ClusterFormComponent implements OnInit, OnDestroy, AfterViewInit { clusterForm: FormGroup = new FormGroup({ id: new FormControl(), name: new FormControl('', { - validators: [Validators.required], + validators: [Validators.required, this.noWhitespaces()], asyncValidators: this.nameShouldBeUnique(), updateOn: 'change' }), @@ -198,6 +199,7 @@ export class ClusterFormComponent implements OnInit, OnDestroy, AfterViewInit { this.navigateToList(); })); } else { + this.model.name = this.model.name.trim(); this.subscriptions.add(this.clusterService.addNewCluster$(this.model).subscribe(() => { this.navigateToList(); })); @@ -234,4 +236,10 @@ export class ClusterFormComponent implements OnInit, OnDestroy, AfterViewInit { })); }; } + + noWhitespaces(): ValidatorFn { + return (control: AbstractControl): ValidationErrors | null => { + return control.value.length === 0 || (control.value || '').trim().length ? null : {incorrectValue: true}; + }; + } }