From 554218d11e1e5f888190cf781dc960e7ff3cabf7 Mon Sep 17 00:00:00 2001 From: Vito Albano Date: Fri, 9 Aug 2024 15:01:44 +0100 Subject: [PATCH] Revert "AAE-23521 Fix improve dropdown reactive form" (#10068) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Revert "AAE-23521 Fix improve dropdown reactive form (#10040)" This reverts commit f88ff10f30b72748bc63521c104200d2c324b56b. * Revert "AAE-24371 Fix invalid endpoint is called when process with form is st… (#10052)" This reverts commit d38e782fe34c64ae6666d89eca467ff1ee629061. * Revert "Revert "AAE-24371 Fix invalid endpoint is called when process with form is st… (#10052)"" This reverts commit 125ada76f61ab7a258503dbc51d09950073f9a95. --- .../widgets/core/form-field-types.ts | 2 +- .../widgets/core/form-field-validator.spec.ts | 45 +++ .../widgets/core/form-field-validator.ts | 17 + .../widgets/error/error.component.scss | 1 - .../dropdown/dropdown-cloud.widget.html | 71 ++-- .../dropdown/dropdown-cloud.widget.scss | 6 - .../dropdown/dropdown-cloud.widget.spec.ts | 70 +--- .../widgets/dropdown/dropdown-cloud.widget.ts | 346 ++++++++---------- .../src/lib/form/form-cloud.module.ts | 3 + .../src/lib/form/form.component.ts | 16 +- .../widgets/dropdown/dropdown.widget.html | 25 +- .../widgets/dropdown/dropdown.widget.spec.ts | 44 +-- .../form/widgets/dropdown/dropdown.widget.ts | 151 ++------ 13 files changed, 331 insertions(+), 466 deletions(-) diff --git a/lib/core/src/lib/form/components/widgets/core/form-field-types.ts b/lib/core/src/lib/form/components/widgets/core/form-field-types.ts index 3bda51016d..f62cfd6ffe 100644 --- a/lib/core/src/lib/form/components/widgets/core/form-field-types.ts +++ b/lib/core/src/lib/form/components/widgets/core/form-field-types.ts @@ -54,7 +54,7 @@ export class FormFieldTypes { static VALIDATABLE_TYPES: string[] = [FormFieldTypes.DISPLAY_EXTERNAL_PROPERTY]; - static REACTIVE_TYPES: string[] = [FormFieldTypes.DATE, FormFieldTypes.DATETIME, FormFieldTypes.DROPDOWN]; + static REACTIVE_TYPES: string[] = [FormFieldTypes.DATE, FormFieldTypes.DATETIME]; static CONSTANT_VALUE_TYPES: string[] = [FormFieldTypes.DISPLAY_EXTERNAL_PROPERTY]; diff --git a/lib/core/src/lib/form/components/widgets/core/form-field-validator.spec.ts b/lib/core/src/lib/form/components/widgets/core/form-field-validator.spec.ts index 1de609d2d3..48513ffdc9 100644 --- a/lib/core/src/lib/form/components/widgets/core/form-field-validator.spec.ts +++ b/lib/core/src/lib/form/components/widgets/core/form-field-validator.spec.ts @@ -59,6 +59,51 @@ describe('FormFieldValidator', () => { expect(validator.validate(field)).toBe(true); }); + it('should fail (display error) for multiple type dropdown with zero selection', () => { + const field = new FormFieldModel(new FormModel(), { + type: FormFieldTypes.DROPDOWN, + value: [{ id: 'id_cat', name: 'Cat' }], + required: true, + selectionType: 'multiple', + hasEmptyValue: false, + options: [ + { id: 'id_cat', name: 'Cat' }, + { id: 'id_dog', name: 'Dog' } + ] + }); + + const validateBeforeUnselect = validator.validate(field); + field.value = []; + const validateAfterUnselect = validator.validate(field); + + expect(validateBeforeUnselect).toBe(true); + expect(validateAfterUnselect).toBe(false); + }); + + it('should fail (display error) for dropdown with null value', () => { + const field = new FormFieldModel(new FormModel(), { + type: FormFieldTypes.DROPDOWN, + value: null, + required: true, + options: [{ id: 'one', name: 'one' }], + selectionType: 'multiple' + }); + + expect(validator.validate(field)).toBe(false); + }); + + it('should fail (display error) for dropdown with empty object', () => { + const field = new FormFieldModel(new FormModel(), { + type: FormFieldTypes.DROPDOWN, + value: {}, + required: true, + options: [{ id: 'one', name: 'one' }], + selectionType: 'multiple' + }); + + expect(validator.validate(field)).toBe(false); + }); + it('should fail (display error) for radio buttons', () => { const field = new FormFieldModel(new FormModel(), { type: FormFieldTypes.RADIO_BUTTONS, diff --git a/lib/core/src/lib/form/components/widgets/core/form-field-validator.ts b/lib/core/src/lib/form/components/widgets/core/form-field-validator.ts index 01804b2503..aa28adafda 100644 --- a/lib/core/src/lib/form/components/widgets/core/form-field-validator.ts +++ b/lib/core/src/lib/form/components/widgets/core/form-field-validator.ts @@ -33,6 +33,7 @@ export class RequiredFieldValidator implements FormFieldValidator { FormFieldTypes.NUMBER, FormFieldTypes.BOOLEAN, FormFieldTypes.TYPEAHEAD, + FormFieldTypes.DROPDOWN, FormFieldTypes.PEOPLE, FormFieldTypes.FUNCTIONAL_GROUP, FormFieldTypes.RADIO_BUTTONS, @@ -50,6 +51,22 @@ export class RequiredFieldValidator implements FormFieldValidator { validate(field: FormFieldModel): boolean { if (this.isSupported(field) && field.isVisible) { + if (field.type === FormFieldTypes.DROPDOWN) { + if (field.hasMultipleValues) { + return Array.isArray(field.value) && !!field.value.length; + } + + if (field.hasEmptyValue && field.emptyOption) { + if (field.value === field.emptyOption.id) { + return false; + } + } + + if (field.required && field.value && typeof field.value === 'object' && !Object.keys(field.value).length) { + return false; + } + } + if (field.type === FormFieldTypes.RADIO_BUTTONS) { const option = field.options.find((opt) => opt.id === field.value); return !!option; diff --git a/lib/core/src/lib/form/components/widgets/error/error.component.scss b/lib/core/src/lib/form/components/widgets/error/error.component.scss index 1427d3d9d2..4687c05c03 100644 --- a/lib/core/src/lib/form/components/widgets/error/error.component.scss +++ b/lib/core/src/lib/form/components/widgets/error/error.component.scss @@ -1,6 +1,5 @@ .adf-error { display: flex; - align-items: center; &-widget-container { height: auto; diff --git a/lib/process-services-cloud/src/lib/form/components/widgets/dropdown/dropdown-cloud.widget.html b/lib/process-services-cloud/src/lib/form/components/widgets/dropdown/dropdown-cloud.widget.html index a06b66f5e0..27fe1892fb 100644 --- a/lib/process-services-cloud/src/lib/form/components/widgets/dropdown/dropdown-cloud.widget.html +++ b/lib/process-services-cloud/src/lib/form/components/widgets/dropdown/dropdown-cloud.widget.html @@ -1,50 +1,43 @@ -
-
-