mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
AAE-30563 Required Form Drop down with default selection (#10614)
This commit is contained in:
@@ -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 })
|
||||
|
@@ -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 };
|
||||
};
|
Reference in New Issue
Block a user