From fb275d94c0ffee673cd933296cd882fe9caf5f1a Mon Sep 17 00:00:00 2001 From: Tomasz Gnyp <49343696+tomgny@users.noreply.github.com> Date: Tue, 25 Jun 2024 11:59:28 +0200 Subject: [PATCH] =?UTF-8?q?AAE-23379=20Fix=20-=20Required=20empty=20attach?= =?UTF-8?q?=20file=20field=20allows=20user=20to=20submi=E2=80=A6=20(#9865)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * AAE-23379 Fix - Required empty attach file field allows user to submit form * small update --- .../components/task-form-cloud.component.html | 1 + .../task-form-cloud.component.spec.ts | 19 ++++++++++++- .../components/task-form-cloud.component.ts | 16 +++++++++-- .../task-form/mocks/task-form-cloud.mock.ts | 28 +++++++++++++++++++ 4 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 lib/process-services-cloud/src/lib/task/task-form/mocks/task-form-cloud.mock.ts diff --git a/lib/process-services-cloud/src/lib/task/task-form/components/task-form-cloud.component.html b/lib/process-services-cloud/src/lib/task/task-form/components/task-form-cloud.component.html index 2f843570ae..993b51a7d4 100644 --- a/lib/process-services-cloud/src/lib/task/task-form/components/task-form-cloud.component.html +++ b/lib/process-services-cloud/src/lib/task/task-form/components/task-form-cloud.component.html @@ -11,6 +11,7 @@ [showCompleteButton]="canCompleteTask()" [showSaveButton]="canCompleteTask()" [displayModeConfigurations]="displayModeConfigurations" + [fieldValidators]="fieldValidators" (formSaved)="onFormSaved($event)" (formCompleted)="onFormCompleted($event)" (formError)="onError($event)" diff --git a/lib/process-services-cloud/src/lib/task/task-form/components/task-form-cloud.component.spec.ts b/lib/process-services-cloud/src/lib/task/task-form/components/task-form-cloud.component.spec.ts index b42c8b40ab..cbba6d6888 100644 --- a/lib/process-services-cloud/src/lib/task/task-form/components/task-form-cloud.component.spec.ts +++ b/lib/process-services-cloud/src/lib/task/task-form/components/task-form-cloud.component.spec.ts @@ -19,7 +19,7 @@ import { DebugElement, SimpleChange } from '@angular/core'; import { By } from '@angular/platform-browser'; import { of } from 'rxjs'; import { ComponentFixture, TestBed } from '@angular/core/testing'; -import { FormModel, FormOutcomeEvent, FormOutcomeModel } from '@alfresco/adf-core'; +import { FORM_FIELD_VALIDATORS, FormModel, FormOutcomeEvent, FormOutcomeModel } from '@alfresco/adf-core'; import { ProcessServiceCloudTestingModule } from '../../../testing/process-service-cloud.testing.module'; import { TaskFormCloudComponent } from './task-form-cloud.component'; import { @@ -37,6 +37,7 @@ import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed'; import { MatProgressSpinnerHarness } from '@angular/material/progress-spinner/testing'; import { DisplayModeService } from '../../../form/services/display-mode.service'; import { FormCloudComponent } from '../../../form/components/form-cloud.component'; +import { MockFormFieldValidator } from '../mocks/task-form-cloud.mock'; const taskDetails: TaskDetailsCloudModel = { appName: 'simple-app', @@ -280,6 +281,22 @@ describe('TaskFormCloudComponent', () => { component.ngOnChanges({ taskId: new SimpleChange(null, 'task1', false) }); expect(getTaskSpy).not.toHaveBeenCalled(); }); + + it('should append additional field validators to the default ones when provided', () => { + const mockFirstCustomFieldValidator = new MockFormFieldValidator(); + const mockSecondCustomFieldValidator = new MockFormFieldValidator(); + + component.fieldValidators = [mockFirstCustomFieldValidator, mockSecondCustomFieldValidator]; + fixture.detectChanges(); + + expect(component.fieldValidators).toEqual([...FORM_FIELD_VALIDATORS, mockFirstCustomFieldValidator, mockSecondCustomFieldValidator]); + }); + + it('should use default field validators when no additional validators are provided', () => { + fixture.detectChanges(); + + expect(component.fieldValidators).toEqual([...FORM_FIELD_VALIDATORS]); + }); }); describe('Events', () => { diff --git a/lib/process-services-cloud/src/lib/task/task-form/components/task-form-cloud.component.ts b/lib/process-services-cloud/src/lib/task/task-form/components/task-form-cloud.component.ts index 857567c859..5a3097c4dd 100644 --- a/lib/process-services-cloud/src/lib/task/task-form/components/task-form-cloud.component.ts +++ b/lib/process-services-cloud/src/lib/task/task-form/components/task-form-cloud.component.ts @@ -18,7 +18,7 @@ import { Component, EventEmitter, Input, OnChanges, Output, SimpleChanges, OnInit, ViewEncapsulation, OnDestroy, ViewChild } from '@angular/core'; import { TaskDetailsCloudModel } from '../../start-task/models/task-details-cloud.model'; import { TaskCloudService } from '../../services/task-cloud.service'; -import { FormRenderingService, FormModel, ContentLinkModel, FormOutcomeEvent } from '@alfresco/adf-core'; +import { FormRenderingService, FormModel, ContentLinkModel, FormOutcomeEvent, FormFieldValidator, FORM_FIELD_VALIDATORS } from '@alfresco/adf-core'; import { AttachFileCloudWidgetComponent } from '../../../form/components/widgets/attach-file/attach-file-cloud-widget.component'; import { DropdownCloudWidgetComponent } from '../../../form/components/widgets/dropdown/dropdown-cloud.widget'; import { DateCloudWidgetComponent } from '../../../form/components/widgets/date/date-cloud.widget'; @@ -72,6 +72,10 @@ export class TaskFormCloudComponent implements OnInit, OnChanges, OnDestroy { @Input() displayModeConfigurations: FormCloudDisplayModeConfiguration[]; + /** FormFieldValidator allow to provide additional validators to the form field. */ + @Input() + fieldValidators: FormFieldValidator[]; + /** Emitted when the form is saved. */ @Output() formSaved = new EventEmitter(); @@ -143,6 +147,8 @@ export class TaskFormCloudComponent implements OnInit, OnChanges, OnDestroy { } ngOnInit() { + this.initFieldValidators(); + if (this.appName === '' && this.taskId) { this.loadTask(); } @@ -162,6 +168,10 @@ export class TaskFormCloudComponent implements OnInit, OnChanges, OnDestroy { } } + private initFieldValidators() { + this.fieldValidators = this.fieldValidators ? [...FORM_FIELD_VALIDATORS, ...this.fieldValidators] : [...FORM_FIELD_VALIDATORS]; + } + private loadTask() { this.loading = true; this.taskCloudService @@ -254,11 +264,11 @@ export class TaskFormCloudComponent implements OnInit, OnChanges, OnDestroy { this.adfCloudForm.switchToDisplayMode(newDisplayMode); } - onDisplayModeOn(displayModeConfiguration: FormCloudDisplayModeConfiguration){ + onDisplayModeOn(displayModeConfiguration: FormCloudDisplayModeConfiguration) { this.displayModeOn.emit(displayModeConfiguration); } - onDisplayModeOff(displayModeConfiguration: FormCloudDisplayModeConfiguration){ + onDisplayModeOff(displayModeConfiguration: FormCloudDisplayModeConfiguration) { this.displayModeOff.emit(displayModeConfiguration); } diff --git a/lib/process-services-cloud/src/lib/task/task-form/mocks/task-form-cloud.mock.ts b/lib/process-services-cloud/src/lib/task/task-form/mocks/task-form-cloud.mock.ts new file mode 100644 index 0000000000..376ecabff9 --- /dev/null +++ b/lib/process-services-cloud/src/lib/task/task-form/mocks/task-form-cloud.mock.ts @@ -0,0 +1,28 @@ +/*! + * @license + * Copyright © 2005-2024 Hyland Software, Inc. and its affiliates. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { FormFieldModel, FormFieldValidator } from '@alfresco/adf-core'; + +export class MockFormFieldValidator implements FormFieldValidator { + isSupported(_field: FormFieldModel): boolean { + return true; + } + + validate(_field: FormFieldModel): boolean { + return true; + } +}