Skip to content

Commit

Permalink
Merge pull request #26271 from software-mansion-labs/fix/last-date-of…
Browse files Browse the repository at this point in the history
…-birth

Fix - user not able to save last enable date of birth
  • Loading branch information
Gonals authored Sep 19, 2023
2 parents d1b48ce + 280d0eb commit e5768e1
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/libs/ValidationUtils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {subYears, addYears, startOfDay, endOfMonth, isAfter, isBefore, isValid, isWithinInterval, isSameDay, format} from 'date-fns';
import {subYears, addYears, startOfDay, endOfMonth, parse, isAfter, isBefore, isValid, isWithinInterval, isSameDay, format} from 'date-fns';
import _ from 'underscore';
import {URL_REGEX_WITH_REQUIRED_PROTOCOL} from 'expensify-common/lib/Url';
import {parsePhoneNumber} from 'awesome-phonenumber';
Expand Down Expand Up @@ -224,19 +224,24 @@ function meetsMaximumAgeRequirement(date) {
*/
function getAgeRequirementError(date, minimumAge, maximumAge) {
const currentDate = startOfDay(new Date());
const recentDate = subYears(currentDate, minimumAge);
const longAgoDate = subYears(currentDate, maximumAge);
const testDate = new Date(date);
const testDate = parse(date, CONST.DATE.FNS_FORMAT_STRING, currentDate);

if (!isValid(testDate)) {
return 'common.error.dateInvalid';
}
if (isWithinInterval(testDate, {start: longAgoDate, end: recentDate})) {

const maximalDate = subYears(currentDate, minimumAge);
const minimalDate = subYears(currentDate, maximumAge);

if (isWithinInterval(testDate, {start: minimalDate, end: maximalDate})) {
return '';
}
if (isSameDay(testDate, recentDate) || isAfter(testDate, recentDate)) {
return ['privatePersonalDetails.error.dateShouldBeBefore', {dateString: format(recentDate, CONST.DATE.FNS_FORMAT_STRING)}];

if (isSameDay(testDate, maximalDate) || isAfter(testDate, maximalDate)) {
return ['privatePersonalDetails.error.dateShouldBeBefore', {dateString: format(maximalDate, CONST.DATE.FNS_FORMAT_STRING)}];
}
return ['privatePersonalDetails.error.dateShouldBeAfter', {dateString: format(longAgoDate, CONST.DATE.FNS_FORMAT_STRING)}];

return ['privatePersonalDetails.error.dateShouldBeAfter', {dateString: format(minimalDate, CONST.DATE.FNS_FORMAT_STRING)}];
}

/**
Expand Down

0 comments on commit e5768e1

Please sign in to comment.