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#3443] Date validation #548

Merged
merged 2 commits into from
Sep 20, 2023
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
13 changes: 13 additions & 0 deletions src/formio/components/DateField.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {Formio} from 'react-formio';

import MinMaxDateValidator from 'formio/validators/minMaxDateValidator';

const DateTimeField = Formio.Components.components.datetime;

const extractDate = value => {
Expand All @@ -11,6 +13,17 @@ const extractDate = value => {
};

class DateField extends DateTimeField {
constructor(component, options, data) {
super(component, options, data);

if (component.datePicker.minDate || component.datePicker.maxDate) {
component.validate.dateMinMax = true;
}

this.validators.push('dateMinMax');
this.validator.validators['dateMinMax'] = MinMaxDateValidator;
}

get suffix() {
// Don't show an icon
return null;
Expand Down
59 changes: 59 additions & 0 deletions src/formio/components/DateField.stories.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import {expect} from '@storybook/jest';
import {userEvent, within} from '@storybook/testing-library';

import {withUtrechtDocument} from 'story-utils/decorators';

import {SingleFormioComponent} from './story-util';
Expand Down Expand Up @@ -38,5 +41,61 @@ export const DateField = {
args: {
key: 'date',
label: 'Datum',
extraComponentProperties: {
format: 'dd-MM-yyyy',
placeholder: 'dd-mm-yyyy',
enableTime: false,
datePicker: {
minDate: null,
maxDate: null,
},
},
},
play: async ({canvasElement}) => {
const canvas = within(canvasElement);

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

userEvent.type(dateInput, '06-06-2006');
sergei-maertens marked this conversation as resolved.
Show resolved Hide resolved
await expect(dateInput).toHaveDisplayValue('06-06-2006');

const error = canvas.queryByText('minDate');
await expect(error).toBeNull();
// This test succeeds, but the value is not displayed in storybook... Mystery
},
};

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

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

userEvent.type(dateInput, '06-06-2006');
SilviaAmAm marked this conversation as resolved.
Show resolved Hide resolved
await expect(dateInput).toHaveDisplayValue('06-06-2006');
SilviaAmAm marked this conversation as resolved.
Show resolved Hide resolved

// TODO: I cannot get this to work. If you do it manually in storybook, it works... (it shows the error).
// const error = canvas.queryByText('minDate');
// await expect(error).not.toBeNull();
sergei-maertens marked this conversation as resolved.
Show resolved Hide resolved
},
};
47 changes: 47 additions & 0 deletions src/formio/validators/minMaxDateValidator.js
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we can add some jest unit tests for the validator itself so we at least have coverage on them?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good idea, I will try!

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import set from 'lodash/set';

const validateDateBoundaries = (minBoundary, maxBoundary, value) => {
const minDate = minBoundary ? new Date(minBoundary) : null;
const maxDate = maxBoundary ? new Date(maxBoundary) : null;

if (!minDate && !maxDate) {
return {isValid: true};

Check warning on line 8 in src/formio/validators/minMaxDateValidator.js

View check run for this annotation

Codecov / codecov/patch

src/formio/validators/minMaxDateValidator.js#L8

Added line #L8 was not covered by tests
}

const parsedValue = new Date(value);

Check warning on line 11 in src/formio/validators/minMaxDateValidator.js

View check run for this annotation

Codecov / codecov/patch

src/formio/validators/minMaxDateValidator.js#L11

Added line #L11 was not covered by tests

if (minDate) return {isValid: parsedValue >= minDate, errorKeys: ['minDate']};
if (maxDate) return {isValid: parsedValue < maxDate, errorKeys: ['maxDate']};
};

const MinMaxDateValidator = {
key: 'validate.dateMinMax',
message(component) {
const minDate = new Date(component.component.minDate);
const maxDate = new Date(component.component.maxDate);

Check warning on line 21 in src/formio/validators/minMaxDateValidator.js

View check run for this annotation

Codecov / codecov/patch

src/formio/validators/minMaxDateValidator.js#L19-L21

Added lines #L19 - L21 were not covered by tests

const errorKeys = component?.openForms?.validationErrorContext?.minMaxDateValidatorErrorKeys;

Check warning on line 23 in src/formio/validators/minMaxDateValidator.js

View check run for this annotation

Codecov / codecov/patch

src/formio/validators/minMaxDateValidator.js#L23

Added line #L23 was not covered by tests
const errorMessage = errorKeys ? errorKeys[0] : 'invalidDate';

return component.t(errorMessage, {

Check warning on line 26 in src/formio/validators/minMaxDateValidator.js

View check run for this annotation

Codecov / codecov/patch

src/formio/validators/minMaxDateValidator.js#L26

Added line #L26 was not covered by tests
minDate: minDate,
maxDate: maxDate,
});
},
check(component, setting, value) {
if (!value) return true;

const {isValid, errorKeys} = validateDateBoundaries(

Check warning on line 34 in src/formio/validators/minMaxDateValidator.js

View check run for this annotation

Codecov / codecov/patch

src/formio/validators/minMaxDateValidator.js#L34

Added line #L34 was not covered by tests
component.component.datePicker.minDate,
component.component.datePicker.maxDate,
value
);

if (!isValid) {
set(component, 'openForms.validationErrorContext.minMaxDateValidatorErrorKeys', errorKeys);

Check warning on line 41 in src/formio/validators/minMaxDateValidator.js

View check run for this annotation

Codecov / codecov/patch

src/formio/validators/minMaxDateValidator.js#L41

Added line #L41 was not covered by tests
}
return isValid;

Check warning on line 43 in src/formio/validators/minMaxDateValidator.js

View check run for this annotation

Codecov / codecov/patch

src/formio/validators/minMaxDateValidator.js#L43

Added line #L43 was not covered by tests
},
};

export default MinMaxDateValidator;
Loading