From 17fd5142924ec26d097345b1d925e02a68282bfe Mon Sep 17 00:00:00 2001 From: jamil314 Date: Thu, 30 Jan 2025 21:14:58 +0600 Subject: [PATCH] fix: unexpected validation errors when days or months are single digit --- .../components/forms/FormFieldGenerator.tsx | 42 ++++++++++++++++++- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/packages/client/src/v2-events/components/forms/FormFieldGenerator.tsx b/packages/client/src/v2-events/components/forms/FormFieldGenerator.tsx index 6849782238..6cecac5130 100644 --- a/packages/client/src/v2-events/components/forms/FormFieldGenerator.tsx +++ b/packages/client/src/v2-events/components/forms/FormFieldGenerator.tsx @@ -462,6 +462,10 @@ class FormSectionComponent extends React.Component { ...field, id: field.id.replaceAll('.', FIELD_SEPARATOR) })) + const valuesWithFormattedDate = getValuesWithFormattedDate( + fieldsWithDotIds, + values + ) return (
@@ -477,7 +481,9 @@ class FormSectionComponent extends React.Component { const conditionalActions: string[] = getConditionalActionsForField( field, { - $form: makeFormikFieldIdsOpenCRVSCompatible(values), + $form: makeFormikFieldIdsOpenCRVSCompatible( + valuesWithFormattedDate + ), $now: formatISO(new Date(), { representation: 'date' }) } ) @@ -548,6 +554,36 @@ function makeFormikFieldIdsOpenCRVSCompatible(data: Record) { ]) ) } +/** + * + * @param fields field config in OpenCRVS format (separated with `.`) + * @param values form values in formik format (separated with `FIELD_SEPARATOR`) + * @returns adds 0 before single digit days and months to make them 2 digit + * @because ajv's `formatMaximum` and `formatMinimum` does not allow single digit day or months + */ +function getValuesWithFormattedDate( + fields: FieldConfig[], + values: Record +) { + return fields.reduce( + (acc, field) => { + const fieldId = field.id.replaceAll('.', FIELD_SEPARATOR) + + if (field.type === 'DATE' && fieldId in values) { + const value = values[fieldId as keyof typeof values] + if (typeof value === 'string') { + const formattedDate = value + .split('-') + .map((d: string) => d.padStart(2, '0')) + .join('-') + acc[fieldId] = formattedDate + } + } + return acc + }, + { ...values } + ) +} export const FormFieldGenerator: React.FC = (props) => { const intl = useIntl() @@ -569,7 +605,9 @@ export const FormFieldGenerator: React.FC = (props) => { validate={(values) => getValidationErrorsForForm( props.fields, - makeFormikFieldIdsOpenCRVSCompatible(values), + makeFormikFieldIdsOpenCRVSCompatible( + getValuesWithFormattedDate(props.fields, values) + ), props.requiredErrorMessage ) }