-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
blocking submit based on rules (#3677)
- Loading branch information
1 parent
7ec7f49
commit ae5d8a9
Showing
3 changed files
with
46 additions
and
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,28 @@ | ||
import { ruleMessage } from '../../helpers/ruleMessage' | ||
import { isDateAfter } from '../../functions/validation/isDateAfter/index.ts' | ||
import { | ||
ValidationRule, | ||
ValidationResult, | ||
ErrorMessages, | ||
Value, | ||
} from '../types' | ||
import { defaultErrorMessages } from './locales' | ||
import { TODAY } from '../../constants' | ||
import { ruleMessage } from '../../helpers/ruleMessage'; | ||
import { isDateAfter } from '../../functions/validation/isDateAfter/index.ts'; | ||
import { ValidationRule, ValidationResult, ErrorMessages, Value } from '../types'; | ||
import { defaultErrorMessages } from './locales'; | ||
import { TODAY } from '../../constants'; | ||
|
||
/** Check that the value is not after today (DD/MM/YYYY format) */ | ||
export function notAfterTodayFn( | ||
errorMessages: ErrorMessages = defaultErrorMessages | ||
): ValidationRule { | ||
function formatDateToDDMMYYYY(date: Date): string { | ||
const day = String(date.getDate()).padStart(2, '0'); | ||
const month = String(date.getMonth() + 1).padStart(2, '0'); | ||
const year = date.getFullYear(); | ||
return `${day}/${month}/${year}`; | ||
} | ||
|
||
/** Vérifie que la valeur n'est pas après aujourd'hui (format DD/MM/YYYY) */ | ||
export function notAfterTodayFn(errorMessages: ErrorMessages = defaultErrorMessages): ValidationRule { | ||
return (value: Value): ValidationResult => { | ||
if (!value) { | ||
return true | ||
return true; | ||
} | ||
|
||
return ( | ||
!isDateAfter(TODAY, value) || ruleMessage(errorMessages, 'default') | ||
) | ||
} | ||
const formattedValue = typeof value === 'object' ? formatDateToDDMMYYYY(value) : value; | ||
if (isDateAfter(TODAY, formattedValue)) { | ||
return ruleMessage(errorMessages, 'default'); | ||
} | ||
return true; | ||
}; | ||
} | ||
|
||
export const notAfterToday = notAfterTodayFn() | ||
export const notAfterToday = notAfterTodayFn(); |
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