From 7c2aeb060bf99d94f52c6178ace25269d8224cd6 Mon Sep 17 00:00:00 2001 From: wiktord2000 Date: Tue, 11 Jun 2024 08:40:46 +0200 Subject: [PATCH] AAE-22947 form-field-validator update --- .../widgets/core/form-field-validator.spec.ts | 25 +++++++++++-------- .../widgets/core/form-field-validator.ts | 4 +-- 2 files changed, 16 insertions(+), 13 deletions(-) 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 f478af0b45..82ce0e60e6 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 @@ -16,7 +16,6 @@ */ import { ErrorMessageModel } from './error-message.model'; -import { FormFieldOption } from './form-field-option'; import { FormFieldTypes } from './form-field-types'; import { FixedValueFieldValidator, @@ -68,17 +67,20 @@ describe('FormFieldValidator', () => { it('should fail (display error) for dropdown with empty value', () => { const field = new FormFieldModel(new FormModel(), { type: FormFieldTypes.DROPDOWN, - value: '', - options: [{ id: 'empty', name: 'Choose option...' }], + value: null, + options: [ + { id: 'empty', name: 'Choose option...' }, + { id: 'none_empty', name: 'None empty' } + ], hasEmptyValue: true, required: true }); - field.emptyOption = { id: '' } as FormFieldOption; - expect(validator.validate(field)).toBe(false); + expect(field.value).toEqual({ id: 'empty', name: 'Choose option...' }); + expect(validator.validate(field)).toBeFalsy(); - field.value = ''; - expect(validator.validate(field)).toBe(true); + field.value = { id: 'noneEmpty', name: 'None empty' }; + expect(validator.validate(field)).toBeTruthy(); }); it('should fail (display error) for multiple type dropdown with zero selection', () => { @@ -107,11 +109,12 @@ describe('FormFieldValidator', () => { type: FormFieldTypes.DROPDOWN, value: null, required: true, - options: [{ id: 'one', name: 'one' }], - selectionType: 'multiple' + options: [{ id: 'id_dog', name: 'Dog' }], + selectionType: 'multiple', + hasEmptyValue: false }); - expect(validator.validate(field)).toBe(false); + expect(validator.validate(field)).toBeFalsy(); }); it('should fail (display error) for dropdown with empty object', () => { @@ -119,7 +122,7 @@ describe('FormFieldValidator', () => { type: FormFieldTypes.DROPDOWN, value: {}, required: true, - options: [{ id: 'one', name: 'one' }], + options: [{ id: 'one', name: 'One' }], selectionType: 'multiple' }); 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 ce097b7dd8..c83194c591 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 @@ -60,8 +60,8 @@ export class RequiredFieldValidator implements FormFieldValidator { return Array.isArray(field.value) && !!field.value.length; } - if (field.hasEmptyValue && field.emptyOption) { - if (field.value === field.emptyOption.id) { + if (field.hasEmptyValue && field.emptyValueOption) { + if (field.value.id === field.emptyValueOption.id) { return false; } }