-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #548 from open-formulieren/fix/3443-date-validation
[OF#3443] Date validation
- Loading branch information
Showing
3 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; | ||
} | ||
|
||
const parsedValue = new Date(value); | ||
|
||
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); | ||
|
||
const errorKeys = component?.openForms?.validationErrorContext?.minMaxDateValidatorErrorKeys; | ||
const errorMessage = errorKeys ? errorKeys[0] : 'invalidDate'; | ||
|
||
return component.t(errorMessage, { | ||
minDate: minDate, | ||
maxDate: maxDate, | ||
}); | ||
}, | ||
check(component, setting, value) { | ||
if (!value) return true; | ||
|
||
const {isValid, errorKeys} = validateDateBoundaries( | ||
component.component.datePicker.minDate, | ||
component.component.datePicker.maxDate, | ||
value | ||
); | ||
|
||
if (!isValid) { | ||
set(component, 'openForms.validationErrorContext.minMaxDateValidatorErrorKeys', errorKeys); | ||
} | ||
return isValid; | ||
}, | ||
}; | ||
|
||
export default MinMaxDateValidator; |