From 7cb926cc0d5d8178a27eb24433d75b094481d253 Mon Sep 17 00:00:00 2001 From: Bartosz Sekula Date: Mon, 8 Apr 2024 11:34:10 +0200 Subject: [PATCH 1/2] [AAE-21797] Datetime widget Min/Max values are checked on a field that is not required --- .../widgets/core/form-field.model.spec.ts | 27 +++++++++++++++++++ .../widgets/core/form-field.model.ts | 2 +- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/core/src/lib/form/components/widgets/core/form-field.model.spec.ts b/lib/core/src/lib/form/components/widgets/core/form-field.model.spec.ts index d04ba3a01da..9ee41cfc3e1 100644 --- a/lib/core/src/lib/form/components/widgets/core/form-field.model.spec.ts +++ b/lib/core/src/lib/form/components/widgets/core/form-field.model.spec.ts @@ -313,6 +313,33 @@ describe('FormFieldModel', () => { expect(form.values['datetime']).toEqual(expectedDateTimeFormat); }); + it('should set the value to null when the value is null', () => { + const form = new FormModel(); + const field = new FormFieldModel(form, { + fieldType: 'FormFieldRepresentation', + id: 'datetime', + name: 'date and time', + type: 'datetime', + value: null, + required: false, + readOnly: false, + params: { + field: { + id: 'datetime', + name: 'date and time', + type: 'datetime', + value: 'now', + required: false, + readOnly: false + } + }, + dateDisplayFormat: 'YYYY-MM-DD HH:mm' + }); + + expect(field.value).toBe(null); + expect(form.values['datetime']).toEqual(null); + }); + it('should parse the checkbox set to "true" when it is readonly', () => { const form = new FormModel(); const field = new FormFieldModel(form, { diff --git a/lib/core/src/lib/form/components/widgets/core/form-field.model.ts b/lib/core/src/lib/form/components/widgets/core/form-field.model.ts index e49b79c9f9c..60c3e01e81b 100644 --- a/lib/core/src/lib/form/components/widgets/core/form-field.model.ts +++ b/lib/core/src/lib/form/components/widgets/core/form-field.model.ts @@ -451,7 +451,7 @@ export class FormFieldModel extends FormWidgetModel { this.value = DateFnsUtils.formatDate(new Date(), this.dateDisplayFormat); } - const dateTimeValue = new Date(this.value); + const dateTimeValue = this.value !== null ? new Date(this.value) : null; if (isValidDate(dateTimeValue)) { this.form.values[this.id] = dateTimeValue.toISOString(); From 797b626c5b7320a67050f94189a4380097f7731c Mon Sep 17 00:00:00 2001 From: Bartosz Sekula Date: Mon, 8 Apr 2024 11:53:35 +0200 Subject: [PATCH 2/2] update test --- .../lib/form/components/widgets/core/form-field.model.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/core/src/lib/form/components/widgets/core/form-field.model.spec.ts b/lib/core/src/lib/form/components/widgets/core/form-field.model.spec.ts index 9ee41cfc3e1..53ec7f1317e 100644 --- a/lib/core/src/lib/form/components/widgets/core/form-field.model.spec.ts +++ b/lib/core/src/lib/form/components/widgets/core/form-field.model.spec.ts @@ -328,7 +328,7 @@ describe('FormFieldModel', () => { id: 'datetime', name: 'date and time', type: 'datetime', - value: 'now', + value: null, required: false, readOnly: false }