Skip to content

Commit

Permalink
fix validations
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Jul 18, 2024
1 parent 2177dee commit 4444678
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/components/form/DatePickerField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ export interface DatePickerFieldProps<
> {
name: string
required?: boolean
min?: string | Date
max?: string | Date
validateOptions?: ValidateOptions
}

Expand All @@ -33,8 +31,8 @@ const DatePickerField = <
>({
name,
required,
min,
max,
minDate,
maxDate,
validateOptions,
...otherDatePickerProps
}: DatePickerFieldProps<
Expand All @@ -43,10 +41,24 @@ const DatePickerField = <
>): JSX.Element => {
const dotPath = name.split(".")

function dateToString(date: Dayjs) {
return date.locale("en-gb").format("L")
}

let schema = YupDate()
if (required) schema = schema.required()
if (min) schema = schema.min(min)
if (max) schema = schema.max(max)
if (minDate) {
schema = schema.min(
minDate,
`this field must be greater or equal to ${dateToString(minDate)}`,
)
}
if (maxDate) {
schema = schema.max(
maxDate,
`this field must be less or equal to ${dateToString(maxDate)}`,
)
}

const fieldConfig: FieldConfig = {
name,
Expand Down

0 comments on commit 4444678

Please sign in to comment.