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