From 5ee158964aabd02f391bf8dbd908534960c7ec14 Mon Sep 17 00:00:00 2001 From: Chisomchima Date: Mon, 25 Nov 2024 06:51:11 +0100 Subject: [PATCH 1/2] feat: add description validation --- i18n/en.pot | 60 ++++++++++++----- .../form/fields/DescriptionField.tsx | 64 ++++++++++++++++--- 2 files changed, 101 insertions(+), 23 deletions(-) diff --git a/i18n/en.pot b/i18n/en.pot index 76676005..afe71cb7 100644 --- a/i18n/en.pot +++ b/i18n/en.pot @@ -5,10 +5,8 @@ msgstr "" "Content-Type: text/plain; charset=utf-8\n" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=2; plural=(n != 1)\n" -"POT-Creation-Date: 2024-11-13T12:57:12.003Z\n" -"PO-Revision-Date: 2024-11-13T12:57:12.004Z\n" -"POT-Creation-Date: 2024-11-13T08:29:01.348Z\n" -"PO-Revision-Date: 2024-11-13T08:29:01.348Z\n" +"POT-Creation-Date: 2024-11-24T18:34:16.366Z\n" +"PO-Revision-Date: 2024-11-24T18:34:16.366Z\n" msgid "schemas" msgstr "schemas" @@ -951,15 +949,6 @@ msgstr "Filter available category options" msgid "Filter selected category options" msgstr "Filter selected category options" -msgid "Choose how this category combo will be used to capture and analyze data." -msgstr "Choose how this category combo will be used to capture and analyze data." - -msgid "Skip category total in reports" -msgstr "Skip category total in reports" - -msgid "Choose the categories to include in this category combo." -msgstr "Choose the categories to include in this category combo." - msgid "Available categories" msgstr "Available categories" @@ -972,8 +961,46 @@ msgstr "Filter available categories" msgid "Filter selected categories" msgstr "Filter selected categories" -msgid "At least one category is required" -msgstr "At least one category is required" +msgid "{{count}} category option combinations will be generated." +msgid_plural "{{count}} category option combinations will be generated." +msgstr[0] "{{count}} category option combinations will be generated." +msgstr[1] "{{count}} category option combinations will be generated." + +msgid "More than 4 Categories" +msgstr "More than 4 Categories" + +msgid "A Category combination with more than 4 categories is not recommended." +msgstr "A Category combination with more than 4 categories is not recommended." + +msgid "Identical Category Combination" +msgstr "Identical Category Combination" + +msgid "" +"One or more Category combinations with the same categories already exist in " +"the system. \n" +" It is strongly discouraged to have more than one Category " +"combination with the same categories." +msgstr "" +"One or more Category combinations with the same categories already exist in " +"the system. \n" +" It is strongly discouraged to have more than one Category " +"combination with the same categories." + +msgid "Choose how this category combo will be used to capture and analyze data." +msgstr "Choose how this category combo will be used to capture and analyze data." + +msgid "Skip category total in reports" +msgstr "Skip category total in reports" + +msgid "Choose the categories to include in this category combo." +msgstr "Choose the categories to include in this category combo." + +msgid "" +"The number of generated category option combinations exceeds the limit of " +"{{limit}}" +msgstr "" +"The number of generated category option combinations exceeds the limit of " +"{{limit}}" msgid "Set up the basic information for this category option group set." msgstr "Set up the basic information for this category option group set." @@ -1042,6 +1069,9 @@ msgstr "" "Choose when, and for which organisation units this category option will be " "available." +msgid "Form name should not exceed 230 characters" +msgstr "Form name should not exceed 230 characters" + msgid "End date should be after start date" msgstr "End date should be after start date" diff --git a/src/components/form/fields/DescriptionField.tsx b/src/components/form/fields/DescriptionField.tsx index 4cd08751..98d5a693 100644 --- a/src/components/form/fields/DescriptionField.tsx +++ b/src/components/form/fields/DescriptionField.tsx @@ -1,8 +1,46 @@ import i18n from '@dhis2/d2-i18n' import { TextAreaFieldFF } from '@dhis2/ui' -import React from 'react' -import { Field as FieldRFF } from 'react-final-form' -import { SchemaSection, useCheckMaxLengthFromSchema } from '../../../lib' +import React, { useMemo } from 'react' +import { Field as FieldRFF, useField } from 'react-final-form' +import { useParams } from 'react-router-dom' +import { + composeAsyncValidators, + required, + useCheckMaxLengthFromSchema, + useIsFieldValueUnique, + SchemaSection, +} from '../../../lib' + +function useValidator({ + schemaSection, + fieldName, +}: { + schemaSection: SchemaSection + fieldName: string +}) { + const params = useParams() + const modelId = params.id as string + const checkIsValueTaken = useIsFieldValueUnique({ + model: schemaSection.namePlural, + field: fieldName, + id: modelId, + }) + + const checkMaxLength = useCheckMaxLengthFromSchema( + schemaSection.name, + fieldName + ) + + return useMemo( + () => + composeAsyncValidators([ + checkIsValueTaken, + checkMaxLength, + required, + ]), + [checkIsValueTaken, checkMaxLength] + ) +} export function DescriptionField({ helpText, @@ -11,17 +49,27 @@ export function DescriptionField({ helpText?: string schemaSection: SchemaSection }) { - const validate = useCheckMaxLengthFromSchema(schemaSection.name, 'formName') + const validator = useValidator({ schemaSection, fieldName: 'description' }) + const { meta } = useField('description', { + subscription: { validating: true }, + }) + + const helpString = + helpText || + i18n.t('A description should be clear and provide necessary details.') return ( - + loading={meta.validating} component={TextAreaFieldFF} dataTest="formfields-description" inputWidth="400px" name="description" - label={i18n.t('Description')} - helpText={helpText} - validate={validate} + label={i18n.t('Description (required)', { + fieldLabel: i18n.t('Description'), + })} + helpText={helpString} + validate={(description?: string) => validator(description)} validateFields={[]} /> ) From 231645c609c5bb21ddae3ab5c8e62c5e1ca836eb Mon Sep 17 00:00:00 2001 From: Chisomchima Date: Thu, 28 Nov 2024 15:54:39 +0100 Subject: [PATCH 2/2] fix: check max length for description --- .../form/fields/DescriptionField.tsx | 67 +++---------------- 1 file changed, 11 insertions(+), 56 deletions(-) diff --git a/src/components/form/fields/DescriptionField.tsx b/src/components/form/fields/DescriptionField.tsx index 98d5a693..629ad075 100644 --- a/src/components/form/fields/DescriptionField.tsx +++ b/src/components/form/fields/DescriptionField.tsx @@ -1,46 +1,8 @@ import i18n from '@dhis2/d2-i18n' import { TextAreaFieldFF } from '@dhis2/ui' -import React, { useMemo } from 'react' -import { Field as FieldRFF, useField } from 'react-final-form' -import { useParams } from 'react-router-dom' -import { - composeAsyncValidators, - required, - useCheckMaxLengthFromSchema, - useIsFieldValueUnique, - SchemaSection, -} from '../../../lib' - -function useValidator({ - schemaSection, - fieldName, -}: { - schemaSection: SchemaSection - fieldName: string -}) { - const params = useParams() - const modelId = params.id as string - const checkIsValueTaken = useIsFieldValueUnique({ - model: schemaSection.namePlural, - field: fieldName, - id: modelId, - }) - - const checkMaxLength = useCheckMaxLengthFromSchema( - schemaSection.name, - fieldName - ) - - return useMemo( - () => - composeAsyncValidators([ - checkIsValueTaken, - checkMaxLength, - required, - ]), - [checkIsValueTaken, checkMaxLength] - ) -} +import React from 'react' +import { Field as FieldRFF } from 'react-final-form' +import { SchemaSection, useCheckMaxLengthFromSchema } from '../../../lib' export function DescriptionField({ helpText, @@ -49,27 +11,20 @@ export function DescriptionField({ helpText?: string schemaSection: SchemaSection }) { - const validator = useValidator({ schemaSection, fieldName: 'description' }) - const { meta } = useField('description', { - subscription: { validating: true }, - }) - - const helpString = - helpText || - i18n.t('A description should be clear and provide necessary details.') + const validate = useCheckMaxLengthFromSchema( + schemaSection.name, + 'description' + ) return ( - - loading={meta.validating} + validator(description)} + label={i18n.t('Description')} + helpText={helpText} + validate={validate} validateFields={[]} /> )