diff --git a/lib/process-services-cloud/src/lib/form/components/widgets/dropdown/dropdown-cloud.widget.ts b/lib/process-services-cloud/src/lib/form/components/widgets/dropdown/dropdown-cloud.widget.ts index 71224fc33d..c9b968ba24 100644 --- a/lib/process-services-cloud/src/lib/form/components/widgets/dropdown/dropdown-cloud.widget.ts +++ b/lib/process-services-cloud/src/lib/form/components/widgets/dropdown/dropdown-cloud.widget.ts @@ -41,6 +41,7 @@ import { TaskVariableCloud } from '../../../models/task-variable-cloud.model'; import { FormCloudService } from '../../../services/form-cloud.service'; import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; import { FormUtilsService } from '../../../services/form-utils.service'; +import { defaultValueValidator } from './validators'; export const DEFAULT_OPTION = { id: 'empty', @@ -198,7 +199,14 @@ export class DropdownCloudWidgetComponent extends WidgetComponent implements OnI } private updateFormControlState(): void { - this.dropdownControl.setValidators(this.isRequired() && this.field?.isVisible ? [Validators.required] : []); + const isRequired = this.isRequired(); + + this.dropdownControl.setValidators(this.isRequired && this.field?.isVisible ? [Validators.required] : []); + + const addSelectDefaultOptionValidator = isRequired && this.field.hasEmptyValue; + if (addSelectDefaultOptionValidator) { + this.dropdownControl.addValidators([defaultValueValidator(this.field)]); + } this.field?.readOnly || this.readOnly ? this.dropdownControl.disable({ emitEvent: false }) diff --git a/lib/process-services-cloud/src/lib/form/components/widgets/dropdown/validators.ts b/lib/process-services-cloud/src/lib/form/components/widgets/dropdown/validators.ts new file mode 100644 index 0000000000..639ae71f2c --- /dev/null +++ b/lib/process-services-cloud/src/lib/form/components/widgets/dropdown/validators.ts @@ -0,0 +1,37 @@ +/*! + * @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 } from '@alfresco/adf-core'; +import { ValidatorFn, AbstractControl, ValidationErrors } from '@angular/forms'; +import { DEFAULT_OPTION } from './dropdown-cloud.widget'; + +export const defaultValueValidator = + (filed: FormFieldModel): ValidatorFn => + (control: AbstractControl): ValidationErrors | null => { + const optionsWithNoDefaultValue = filed.options.filter((dropdownOption) => { + const isDefaultValue = dropdownOption.id === DEFAULT_OPTION.id && dropdownOption.name === DEFAULT_OPTION.name; + + return !isDefaultValue; + }); + + const isSomeOptionSelected = optionsWithNoDefaultValue.some((dropdownOption) => { + const isOptionSelected = dropdownOption.id === control.value?.id; + return isOptionSelected; + }); + + return isSomeOptionSelected ? null : { required: true }; + };