AAE-33449 HXPMNT-748 Hidden dropdown validation (#10760)

This commit is contained in:
Robert Duda 2025-04-01 14:49:23 +02:00 committed by Bartosz Sekula
parent 6afe574e13
commit f7696aef74
No known key found for this signature in database
GPG Key ID: BCF7B7A8D56A6450
3 changed files with 65 additions and 23 deletions

View File

@ -18,7 +18,7 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { of, throwError } from 'rxjs';
import { DropdownCloudWidgetComponent } from './dropdown-cloud.widget';
import { DEFAULT_OPTION, DropdownCloudWidgetComponent } from './dropdown-cloud.widget';
import { FormFieldModel, FormModel, FormService, FormFieldEvent, FormFieldTypes } from '@alfresco/adf-core';
import { FormCloudService } from '../../../services/form-cloud.service';
import { ProcessServiceCloudTestingModule } from '../../../../testing/process-service-cloud.testing.module';
@ -375,8 +375,36 @@ describe('DropdownCloudWidgetComponent', () => {
expect(element.querySelector('.adf-invalid')).toBeFalsy();
});
it('should be valid when field is hidden with empty value', () => {
describe('and visible', () => {
beforeEach(() => {
widget.field.isVisible = true;
});
it('should be invalid with no option selected', () => {
fixture.detectChanges();
expect(widget.field.isValid).toBeFalse();
expect(widget.dropdownControl.valid).toBeFalse();
expect(widget.field.validationSummary.message).toBe('FORM.FIELD.REQUIRED');
});
it('should be invalid with default option selected', () => {
widget.field.hasEmptyValue = true;
widget.field.value = DEFAULT_OPTION;
fixture.detectChanges();
expect(widget.field.isValid).toBeFalse();
expect(widget.dropdownControl.valid).toBeFalse();
expect(widget.field.validationSummary.message).toBe('FORM.FIELD.REQUIRED');
});
});
describe('and NOT visible', () => {
beforeEach(() => {
widget.field.isVisible = false;
});
it('should be valid with no option selected', () => {
fixture.detectChanges();
expect(widget.field.isValid).toBeTrue();
@ -384,13 +412,15 @@ describe('DropdownCloudWidgetComponent', () => {
expect(widget.field.validationSummary.message).toBe('');
});
it('should be invalid when field is hidden with empty value', () => {
widget.field.isVisible = true;
it('should be valid with default option selected', () => {
widget.field.hasEmptyValue = true;
widget.field.value = DEFAULT_OPTION;
fixture.detectChanges();
expect(widget.field.isValid).toBeFalse();
expect(widget.dropdownControl.valid).toBeFalse();
expect(widget.field.validationSummary.message).toBe('FORM.FIELD.REQUIRED');
expect(widget.field.isValid).toBeTrue();
expect(widget.dropdownControl.valid).toBeTrue();
expect(widget.field.validationSummary.message).toBe('');
});
});
});

View File

@ -199,20 +199,32 @@ export class DropdownCloudWidgetComponent extends WidgetComponent implements OnI
}
private updateFormControlState(): void {
const isFieldRequired = this.isRequired();
this.dropdownControl.setValidators(isFieldRequired && this.field?.isVisible ? [Validators.required] : []);
const addSelectDefaultOptionValidator = isFieldRequired && this.field.hasEmptyValue;
if (addSelectDefaultOptionValidator) {
this.dropdownControl.addValidators([defaultValueValidator(this.field)]);
this.updateDropdownValidationRules();
this.updateDropdownReadonlyRules();
this.dropdownControl.updateValueAndValidity({ emitEvent: false });
}
this.field?.readOnly || this.readOnly
? this.dropdownControl.disable({ emitEvent: false })
: this.dropdownControl.enable({ emitEvent: false });
private updateDropdownValidationRules() {
this.dropdownControl.setValidators([]);
this.dropdownControl.updateValueAndValidity({ emitEvent: false });
if (!this.field?.isVisible) {
return;
}
if (this.isRequired()) {
this.dropdownControl.addValidators([Validators.required]);
if (this.field.hasEmptyValue) {
this.dropdownControl.addValidators([defaultValueValidator(this.field)]);
}
}
}
private updateDropdownReadonlyRules() {
if (this.field?.readOnly || this.readOnly) {
this.dropdownControl.disable({ emitEvent: false });
} else {
this.dropdownControl.enable({ emitEvent: false });
}
}
private handleErrors(): void {

View File

@ -23,7 +23,7 @@ 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;
const isDefaultValue = dropdownOption.id === DEFAULT_OPTION.id;
return !isDefaultValue;
});