From 7f106f7a563d32c023f339257c403fd60eddb314 Mon Sep 17 00:00:00 2001 From: Cilibiu Bogdan Date: Fri, 21 Dec 2018 20:06:35 +0200 Subject: [PATCH] [ADF-3864] Create library - no error is displayed when name contains white spaces only (#4101) * only spaces error message * title custom error support * tests * validate title against only spaces * generate id only when title is valid --- .../dialogs/library/library.dialog.html | 4 ++ .../dialogs/library/library.dialog.spec.ts | 42 +++++++++++++++++++ .../dialogs/library/library.dialog.ts | 40 ++++++++++++++---- lib/content-services/i18n/en.json | 1 + 4 files changed, 78 insertions(+), 9 deletions(-) diff --git a/lib/content-services/dialogs/library/library.dialog.html b/lib/content-services/dialogs/library/library.dialog.html index b8d1c31629..0a8ea1624a 100644 --- a/lib/content-services/dialogs/library/library.dialog.html +++ b/lib/content-services/dialogs/library/library.dialog.html @@ -18,6 +18,10 @@ {{ 'LIBRARY.ERRORS.TITLE_TOO_LONG' | translate }} + + + {{ form.controls['title'].errors?.message | translate }} + diff --git a/lib/content-services/dialogs/library/library.dialog.spec.ts b/lib/content-services/dialogs/library/library.dialog.spec.ts index e32e881fcd..56fbe59585 100644 --- a/lib/content-services/dialogs/library/library.dialog.spec.ts +++ b/lib/content-services/dialogs/library/library.dialog.spec.ts @@ -214,4 +214,46 @@ describe('LibraryDialogComponent', () => { message: 'LIBRARY.ERRORS.CONFLICT' }); })); + + it('should not translate library title if value is not a valid id', fakeAsync(() => { + spyOn(alfrescoApi.sitesApi, 'getSite').and.callFake(() => { + return new Promise((resolve, reject) => reject()); + }); + + fixture.detectChanges(); + component.form.controls.title.setValue('@@@####'); + tick(500); + flush(); + fixture.detectChanges(); + + expect(component.form.controls.id.value).toBe(null); + })); + + it('should translate library title partially for library id', fakeAsync(() => { + spyOn(alfrescoApi.sitesApi, 'getSite').and.callFake(() => { + return new Promise((resolve, reject) => reject()); + }); + + fixture.detectChanges(); + component.form.controls.title.setValue('@@@####library'); + tick(500); + flush(); + fixture.detectChanges(); + + expect(component.form.controls.id.value).toBe('library'); + })); + + it('should translate library title multiple space character to one dash for library id', fakeAsync(() => { + spyOn(alfrescoApi.sitesApi, 'getSite').and.callFake(() => { + return new Promise((resolve, reject) => reject()); + }); + + fixture.detectChanges(); + component.form.controls.title.setValue('library title'); + tick(500); + flush(); + fixture.detectChanges(); + + expect(component.form.controls.id.value).toBe('library-title'); + })); }); diff --git a/lib/content-services/dialogs/library/library.dialog.ts b/lib/content-services/dialogs/library/library.dialog.ts index 4de459bd23..33f99af0a0 100644 --- a/lib/content-services/dialogs/library/library.dialog.ts +++ b/lib/content-services/dialogs/library/library.dialog.ts @@ -84,13 +84,17 @@ export class LibraryDialogComponent implements OnInit, OnDestroy { Validators.maxLength(72), this.forbidSpecialCharacters ], - title: [Validators.required, Validators.maxLength(256)], + title: [ + Validators.required, + this.forbidOnlySpaces, + Validators.maxLength(256) + ], description: [Validators.maxLength(512)] }; this.form = this.formBuilder.group({ - title: ['', validators.title], - id: ['', validators.id, this.createSiteIdValidator()], + title: [null, validators.title], + id: [null, validators.id, this.createSiteIdValidator()], description: ['', validators.description] }); @@ -106,11 +110,7 @@ export class LibraryDialogComponent implements OnInit, OnDestroy { takeUntil(this.onDestroy$) ) .subscribe((title: string) => { - if (!title.trim().length) { - return; - } - - if (!this.form.controls['id'].dirty) { + if (!this.form.controls['id'].dirty && this.canGenerateId(title)) { this.form.patchValue({ id: this.sanitize(title.trim()) }); this.form.controls['id'].markAsTouched(); } @@ -177,7 +177,11 @@ export class LibraryDialogComponent implements OnInit, OnDestroy { } private sanitize(input: string) { - return input.replace(/[\s]/g, '-').replace(/[^A-Za-z0-9-]/g, ''); + return input.replace(/[\s\s]+/g, '-').replace(/[^A-Za-z0-9-]/g, ''); + } + + private canGenerateId(title) { + return Boolean(title.replace(/[^A-Za-z0-9-]/g, '').length); } private handleError(error: any): any { @@ -215,6 +219,10 @@ export class LibraryDialogComponent implements OnInit, OnDestroy { } private forbidSpecialCharacters({ value }: FormControl) { + if (value === null || value.length === 0) { + return null; + } + const validCharacters: RegExp = /[^A-Za-z0-9-]/; const isValid: boolean = !validCharacters.test(value); @@ -225,6 +233,20 @@ export class LibraryDialogComponent implements OnInit, OnDestroy { }; } + private forbidOnlySpaces({ value }: FormControl) { + if (value === null || value.length === 0) { + return null; + } + + const isValid: boolean = !!(value || '').trim(); + + return isValid + ? null + : { + message: 'LIBRARY.ERRORS.ONLY_SPACES' + }; + } + private createSiteIdValidator() { let timer; diff --git a/lib/content-services/i18n/en.json b/lib/content-services/i18n/en.json index 9760557c75..8cbc64424d 100644 --- a/lib/content-services/i18n/en.json +++ b/lib/content-services/i18n/en.json @@ -323,6 +323,7 @@ "DESCRIPTION_TOO_LONG": "Use 512 characters or less for description", "TITLE_TOO_LONG": "Use 256 characters or less for title", "ILLEGAL_CHARACTERS": "Use numbers and letters only", + "ONLY_SPACES": "Library name can't contain only spaces", "LIBRARY_UPDATE_ERROR": "There was an error updating library properties" }, "SUCCESS": {