AAE-30563 Required Form Drop down with default selection (#10614)

This commit is contained in:
Bartosz Sekula
2025-02-03 05:51:09 -05:00
committed by GitHub
parent 0c459227a8
commit 34855a840a
2 changed files with 46 additions and 1 deletions

View File

@@ -41,6 +41,7 @@ import { TaskVariableCloud } from '../../../models/task-variable-cloud.model';
import { FormCloudService } from '../../../services/form-cloud.service'; import { FormCloudService } from '../../../services/form-cloud.service';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { FormUtilsService } from '../../../services/form-utils.service'; import { FormUtilsService } from '../../../services/form-utils.service';
import { defaultValueValidator } from './validators';
export const DEFAULT_OPTION = { export const DEFAULT_OPTION = {
id: 'empty', id: 'empty',
@@ -198,7 +199,14 @@ export class DropdownCloudWidgetComponent extends WidgetComponent implements OnI
} }
private updateFormControlState(): void { 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.field?.readOnly || this.readOnly
? this.dropdownControl.disable({ emitEvent: false }) ? this.dropdownControl.disable({ emitEvent: false })

View File

@@ -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 };
};