Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OF#3671] Fix max date validation when current included #616

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/formio/components/DateField.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,38 @@ export const DateWithMinField = {
// await expect(error).not.toBeNull();
},
};

export const DateWithMaxField = {
render: SingleFormioComponent,
args: {
key: 'date',
label: 'Datum <= 18-12-2023',
extraComponentProperties: {
format: 'dd-MM-yyyy',
placeholder: 'dd-mm-yyyy',
enableTime: false,
datePicker: {
minDate: null,
maxDate: '2023-12-18T00:00:00+01:00',
},
customOptions: {
allowInvalidPreload: true,
},
validate: {
dateMinMax: true,
},
},
},
play: async ({canvasElement}) => {
const canvas = within(canvasElement);

const dateInput = canvas.getByRole('textbox');

userEvent.type(dateInput, '19-12-2023');
expect(dateInput).toHaveDisplayValue('19-12-2023');

// TODO: I cannot get this to work. If you do it manually in storybook, it works... (it shows the error).
// const error = canvas.queryByText('maxDate');
// await expect(error).not.toBeNull();
},
};
3 changes: 2 additions & 1 deletion src/formio/validators/minMaxDateValidator.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {parseISO} from 'date-fns';
import set from 'lodash/set';

const validateDateBoundaries = (minBoundary, maxBoundary, value) => {
Expand All @@ -8,7 +9,7 @@ const validateDateBoundaries = (minBoundary, maxBoundary, value) => {
return {isValid: true};
}

const parsedValue = new Date(value);
const parsedValue = parseISO(value, 'yyyy-MM-dd', new Date());

if (minDate && maxDate) {
const isValid = parsedValue >= minDate && parsedValue <= maxDate;
Expand Down
24 changes: 24 additions & 0 deletions src/jstests/formio/components/date.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,30 @@ describe('Date Component', () => {
done();
});

test('Date validator: check max date including the current one', done => {
const component = {
label: 'date',
key: 'date',
type: 'date',
datePicker: {
minDate: null,
maxDate: '2023-09-08',
},
customOptions: {
allowInvalidPreload: true,
},
validate: {dateMinMax: true},
};

const componentInstance = new FormioComponent(component, {}, {});

const isValid1 = MinMaxDateValidator.check(componentInstance, {}, '2023-09-08');

expect(isValid1).toBeTruthy();

done();
});

test('Date validator: error message', done => {
const component = {
label: 'date',
Expand Down
Loading