diff --git a/src/formio/validators/minMaxDateValidator.js b/src/formio/validators/minMaxDateValidator.js index 0c74f23ae..65ae2ad2c 100644 --- a/src/formio/validators/minMaxDateValidator.js +++ b/src/formio/validators/minMaxDateValidator.js @@ -10,8 +10,14 @@ 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']}; + if (maxDate) return {isValid: parsedValue <= maxDate, errorKeys: ['maxDate']}; }; const MinMaxDateValidator = { 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(); + }); });