From 26a18f2c8331340d4f8b4fce76c120a165b7fafa Mon Sep 17 00:00:00 2001 From: SilviaAmAm Date: Tue, 21 Nov 2023 10:22:12 +0100 Subject: [PATCH 1/3] :white_check_mark: [open-formulieren/open-forms#3612] Test both min and max validation --- src/jstests/formio/components/date.spec.js | 34 ++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/jstests/formio/components/date.spec.js b/src/jstests/formio/components/date.spec.js index e336ae995..374c79cca 100644 --- a/src/jstests/formio/components/date.spec.js +++ b/src/jstests/formio/components/date.spec.js @@ -110,4 +110,38 @@ describe('Date Component', () => { done(); }); + + test('Date validator: check max date AND min date', done => { + const component = { + label: 'date', + key: 'date', + type: 'date', + datePicker: { + minDate: '2023-09-01', + maxDate: '2023-09-08', + }, + customOptions: { + allowInvalidPreload: true, + }, + validate: {dateMinMax: true}, + }; + + const componentInstance = new FormioComponent(component, {}, {}); + + const isValid1 = MinMaxDateValidator.check(componentInstance, {}, '2024-01-01'); + + expect(isValid1).toBeFalsy(); + expect( + componentInstance.openForms.validationErrorContext.minMaxDateValidatorErrorKeys + ).toContain('maxDate'); + + const isValid2 = MinMaxDateValidator.check(componentInstance, {}, '2020-01-01'); + + expect(isValid2).toBeFalsy(); + expect( + componentInstance.openForms.validationErrorContext.minMaxDateValidatorErrorKeys + ).toContain('minDate'); + + done(); + }); }); From b0ad05463db6b61a7fb2fdfed7e729d50f6c9ebf Mon Sep 17 00:00:00 2001 From: SilviaAmAm Date: Tue, 21 Nov 2023 10:22:39 +0100 Subject: [PATCH 2/3] :bug: [open-formulieren/open-forms#3612] Check both min and max limits if both are present --- src/formio/validators/minMaxDateValidator.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/formio/validators/minMaxDateValidator.js b/src/formio/validators/minMaxDateValidator.js index 0c74f23ae..5d50364a9 100644 --- a/src/formio/validators/minMaxDateValidator.js +++ b/src/formio/validators/minMaxDateValidator.js @@ -10,6 +10,12 @@ const validateDateBoundaries = (minBoundary, maxBoundary, value) => { const parsedValue = new Date(value); + if (minDate && maxDate) { + const isValid = parsedValue >= minDate && parsedValue < maxDate; + let errorKeys = isValid ? [] : parsedValue < minDate ? ['minDate'] : ['maxDate']; + return {isValid, errorKeys}; + } + if (minDate) return {isValid: parsedValue >= minDate, errorKeys: ['minDate']}; if (maxDate) return {isValid: parsedValue < maxDate, errorKeys: ['maxDate']}; }; From 3d0d5eeb5a94715be9c2c4e3867e550a4e433a9c Mon Sep 17 00:00:00 2001 From: SilviaAmAm Date: Tue, 21 Nov 2023 11:10:14 +0100 Subject: [PATCH 3/3] :sparkles: [open-formulieren/open-forms#3612] Make checks inclusive to be consistent with time/number components --- src/formio/validators/minMaxDateValidator.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/formio/validators/minMaxDateValidator.js b/src/formio/validators/minMaxDateValidator.js index 5d50364a9..65ae2ad2c 100644 --- a/src/formio/validators/minMaxDateValidator.js +++ b/src/formio/validators/minMaxDateValidator.js @@ -11,13 +11,13 @@ const validateDateBoundaries = (minBoundary, maxBoundary, value) => { const parsedValue = new Date(value); if (minDate && maxDate) { - const isValid = parsedValue >= minDate && parsedValue < maxDate; + const isValid = parsedValue >= minDate && parsedValue <= maxDate; let errorKeys = isValid ? [] : parsedValue < minDate ? ['minDate'] : ['maxDate']; return {isValid, errorKeys}; } if (minDate) return {isValid: parsedValue >= minDate, errorKeys: ['minDate']}; - if (maxDate) return {isValid: parsedValue < maxDate, errorKeys: ['maxDate']}; + if (maxDate) return {isValid: parsedValue <= maxDate, errorKeys: ['maxDate']}; }; const MinMaxDateValidator = {