From 423494f4f803a9af1c40860db0b31cbe24a8f11c Mon Sep 17 00:00:00 2001 From: siva kumar Date: Mon, 21 Jan 2019 21:30:00 +0530 Subject: [PATCH] [ADF-3889] [Start Process Cloud] - APS2 - error displayed when try to create a process with a name bigger the 255 characters (#4182) * [ADF-3889] [Start Process Cloud] - APS2 - error displayed when try to create an process with an name bigger the 255 characters* Fixed error displayed when try to create an process with an name bigger the 255 characters * * Fixed comments* Updated doc * Added unit tests to the recent changes * * Added validation to check whitespace * Prevented starting a process just with the whitespace * Fixed Error displayed in console when start a process --- .../start-process-cloud.component.md | 1 + .../src/lib/i18n/en.json | 3 ++- .../edit-process-filter-cloud.component.html | 2 +- .../start-process-cloud.component.html | 5 +++- .../start-process-cloud.component.spec.ts | 24 +++++++++++++++++++ .../start-process-cloud.component.ts | 20 ++++++++++++++-- .../components/start-task-cloud.component.ts | 20 ++++++++++------ 7 files changed, 63 insertions(+), 12 deletions(-) diff --git a/docs/process-services-cloud/start-process-cloud.component.md b/docs/process-services-cloud/start-process-cloud.component.md index 1cc45fc045..944f854d5a 100755 --- a/docs/process-services-cloud/start-process-cloud.component.md +++ b/docs/process-services-cloud/start-process-cloud.component.md @@ -26,6 +26,7 @@ Starts a process. | Name | Type | Default value | Description | | ---- | ---- | ------------- | ----------- | | appName | `string` | | (required) Name of the app. | +| maxNameLength | `number` | | Maximum length of the process name. | | name | `string` | "" | Name of the process. | | processDefinitionName | `string` | | Name of the process definition. | | showSelectProcessDropdown | `boolean` | true | Show/hide the process dropdown list. | diff --git a/lib/process-services-cloud/src/lib/i18n/en.json b/lib/process-services-cloud/src/lib/i18n/en.json index eb0e3a9d9f..0f039913fa 100644 --- a/lib/process-services-cloud/src/lib/i18n/en.json +++ b/lib/process-services-cloud/src/lib/i18n/en.json @@ -28,7 +28,8 @@ "LOAD_PROCESS_DEFS": "Couldn't load process definitions, check you have access.", "START": "Couldn't start new process instance, check you have access.", "PROCESS_NAME_REQUIRED": "Process Name is required", - "PROCESS_DEFINITION_REQUIRED": "Process Definition is required" + "PROCESS_DEFINITION_REQUIRED": "Process Definition is required", + "MAXIMUM_LENGTH": "Length exceeded, {{characters}} characters max." } } }, diff --git a/lib/process-services-cloud/src/lib/process/process-filters/components/edit-process-filter-cloud.component.html b/lib/process-services-cloud/src/lib/process/process-filters/components/edit-process-filter-cloud.component.html index 9a5538565a..c93ab1d40b 100644 --- a/lib/process-services-cloud/src/lib/process/process-filters/components/edit-process-filter-cloud.component.html +++ b/lib/process-services-cloud/src/lib/process/process-filters/components/edit-process-filter-cloud.component.html @@ -1,4 +1,4 @@ - + {{processFilter.name | translate}} diff --git a/lib/process-services-cloud/src/lib/process/start-process/components/start-process-cloud.component.html b/lib/process-services-cloud/src/lib/process/start-process/components/start-process-cloud.component.html index 484b47c11f..34ddb20045 100755 --- a/lib/process-services-cloud/src/lib/process/start-process/components/start-process-cloud.component.html +++ b/lib/process-services-cloud/src/lib/process/start-process/components/start-process-cloud.component.html @@ -15,9 +15,12 @@ matInput formControlName="processInstanceName" id="processName"> - + {{ 'ADF_CLOUD_PROCESS_LIST.ADF_CLOUD_START_PROCESS.ERROR.PROCESS_NAME_REQUIRED' | translate }} + + {{ 'ADF_CLOUD_PROCESS_LIST.ADF_CLOUD_START_PROCESS.ERROR.MAXIMUM_LENGTH' | translate : { characters : maxNameLength } }} + diff --git a/lib/process-services-cloud/src/lib/process/start-process/components/start-process-cloud.component.spec.ts b/lib/process-services-cloud/src/lib/process/start-process/components/start-process-cloud.component.spec.ts index 2b1e467b67..4b6e71a5ef 100755 --- a/lib/process-services-cloud/src/lib/process/start-process/components/start-process-cloud.component.spec.ts +++ b/lib/process-services-cloud/src/lib/process/start-process/components/start-process-cloud.component.spec.ts @@ -376,5 +376,29 @@ describe('StartProcessCloudComponent', () => { component.startProcess(); }); + + it('should emit error when process name exceeds maximum length', () => { + component.maxNameLength = 2; + component.ngOnInit(); + fixture.detectChanges(); + let processInstanceName = component.processForm.controls['processInstanceName']; + processInstanceName.setValue('task'); + fixture.detectChanges(); + expect(processInstanceName.valid).toBeFalsy(); + processInstanceName.setValue('ta'); + fixture.detectChanges(); + expect(processInstanceName.valid).toBeTruthy(); + }); + + it('should emit error when process name field is empty', () => { + fixture.detectChanges(); + let processInstanceName = component.processForm.controls['processInstanceName']; + processInstanceName.setValue(''); + fixture.detectChanges(); + expect(processInstanceName.valid).toBeFalsy(); + processInstanceName.setValue('task'); + fixture.detectChanges(); + expect(processInstanceName.valid).toBeTruthy(); + }); }); }); diff --git a/lib/process-services-cloud/src/lib/process/start-process/components/start-process-cloud.component.ts b/lib/process-services-cloud/src/lib/process/start-process/components/start-process-cloud.component.ts index 3156eca63a..4f34d5f529 100755 --- a/lib/process-services-cloud/src/lib/process/start-process/components/start-process-cloud.component.ts +++ b/lib/process-services-cloud/src/lib/process/start-process/components/start-process-cloud.component.ts @@ -36,6 +36,8 @@ import { ProcessDefinitionCloud } from '../models/process-definition-cloud.model }) export class StartProcessCloudComponent implements OnChanges, OnInit { + static MAX_NAME_LENGTH: number = 255; + @ViewChild(MatAutocompleteTrigger) inputAutocomplete: MatAutocompleteTrigger; @@ -43,6 +45,10 @@ export class StartProcessCloudComponent implements OnChanges, OnInit { @Input() appName: string; + /** Maximum length of the process name. */ + @Input() + maxNameLength: number = StartProcessCloudComponent.MAX_NAME_LENGTH; + /** Name of the process. */ @Input() name: string = ''; @@ -77,14 +83,13 @@ export class StartProcessCloudComponent implements OnChanges, OnInit { processPayloadCloud = new ProcessPayloadCloud(); filteredProcesses: ProcessDefinitionCloud[] = []; isLoading = false; - constructor(private startProcessCloudService: StartProcessCloudService, private formBuilder: FormBuilder) { } ngOnInit() { this.processForm = this.formBuilder.group({ - processInstanceName: new FormControl(this.name, Validators.required), + processInstanceName: new FormControl(this.name, [Validators.required, Validators.maxLength(this.getMaxNameLength()), this.whitespaceValidator]), processDefinition: new FormControl('', [Validators.required, this.processDefinitionNameValidator()]) }); @@ -105,6 +110,11 @@ export class StartProcessCloudComponent implements OnChanges, OnInit { } } + private getMaxNameLength(): number { + return this.maxNameLength > StartProcessCloudComponent.MAX_NAME_LENGTH ? + StartProcessCloudComponent.MAX_NAME_LENGTH : this.maxNameLength; + } + setProcessDefinitionOnForm(processDefinitionName: string) { this.filteredProcesses = this.getProcessDefinitionList(processDefinitionName); const selectedProcess = this.getProcessIfExists(processDefinitionName); @@ -227,6 +237,12 @@ export class StartProcessCloudComponent implements OnChanges, OnInit { }; } + public whitespaceValidator(control: FormControl) { + const isWhitespace = (control.value || '').trim().length === 0; + const isValid = !isWhitespace; + return isValid ? null : { 'whitespace': true }; + } + get processInstanceName(): AbstractControl { return this.processForm.get('processInstanceName'); } diff --git a/lib/process-services-cloud/src/lib/task/start-task/components/start-task-cloud.component.ts b/lib/process-services-cloud/src/lib/task/start-task/components/start-task-cloud.component.ts index 88aebd74fd..e24ee1edd4 100644 --- a/lib/process-services-cloud/src/lib/task/start-task/components/start-task-cloud.component.ts +++ b/lib/process-services-cloud/src/lib/task/start-task/components/start-task-cloud.component.ts @@ -33,10 +33,10 @@ import { import { PeopleCloudComponent } from './people-cloud/people-cloud.component'; @Component({ - selector: 'adf-cloud-start-task', - templateUrl: './start-task-cloud.component.html', - styleUrls: ['./start-task-cloud.component.scss'], - providers: [ + selector: 'adf-cloud-start-task', + templateUrl: './start-task-cloud.component.html', + styleUrls: ['./start-task-cloud.component.scss'], + providers: [ { provide: DateAdapter, useClass: MomentDateAdapter }, { provide: MAT_DATE_FORMATS, useValue: MOMENT_DATE_FORMATS }], encapsulation: ViewEncapsulation.None @@ -123,7 +123,7 @@ export class StartTaskCloudComponent implements OnInit, OnDestroy { buildForm() { this.taskForm = this.formBuilder.group({ - name: new FormControl(this.name, [Validators.required, Validators.maxLength(this.getMaxNameLength())]), + name: new FormControl(this.name, [Validators.required, Validators.maxLength(this.getMaxNameLength()), this.whitespaceValidator]), priority: new FormControl(), description: new FormControl() }); @@ -131,7 +131,7 @@ export class StartTaskCloudComponent implements OnInit, OnDestroy { private getMaxNameLength(): number { return this.maxNameLength > StartTaskCloudComponent.MAX_NAME_LENGTH ? - StartTaskCloudComponent.MAX_NAME_LENGTH : this.maxNameLength; + StartTaskCloudComponent.MAX_NAME_LENGTH : this.maxNameLength; } private loadCurrentUser() { @@ -158,7 +158,7 @@ export class StartTaskCloudComponent implements OnInit, OnDestroy { this.submitted = false; this.error.emit(err); this.logService.error('An error occurred while creating new task'); - }); + }); } public onCancel() { @@ -192,6 +192,12 @@ export class StartTaskCloudComponent implements OnInit, OnDestroy { this.assigneeName = assignee ? assignee.username : ''; } + public whitespaceValidator(control: FormControl) { + const isWhitespace = (control.value || '').trim().length === 0; + const isValid = !isWhitespace; + return isValid ? null : { 'whitespace': true }; + } + get nameController(): AbstractControl { return this.taskForm.get('name'); }