Skip to content

Commit

Permalink
fix: add validation for myinfo child name field (#7875)
Browse files Browse the repository at this point in the history
* fix: add validation rules to subfields and name field

* fix: name the index as child name index
  • Loading branch information
kevin9foong authored Nov 11, 2024
1 parent 201e5ed commit b23dce1
Showing 1 changed file with 19 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useEffect, useMemo, useRef } from 'react'
import { useCallback, useEffect, useMemo } from 'react'
import {
Controller,
FieldArrayWithId,
Expand Down Expand Up @@ -36,6 +36,7 @@ import {
} from '~shared/types'
import { formatMyinfoDate } from '~shared/utils/dates'

import { REQUIRED_ERROR } from '~constants/validation'
import { createChildrenValidationRules } from '~utils/fieldValidation'
import { Button } from '~components/Button/Button'
import { DatePicker } from '~components/DatePicker'
Expand Down Expand Up @@ -202,6 +203,8 @@ interface ChildrenBodyProps {
error: FieldError[] | undefined
}

const CHILD_NAME_INDEX = 0

const ChildrenBody = ({
currChildBodyIdx,
schema,
Expand All @@ -222,19 +225,11 @@ const ChildrenBody = ({
[schema._id, currChildBodyIdx],
)

const validationRules = useMemo(
const childrenValidationRules = useMemo(
() => createChildrenValidationRules(schema, disableRequiredValidation),
[schema, disableRequiredValidation],
)

const childNameRef = useRef<HTMLInputElement | null>(null)

const childNameError = error
? error.find(
(e) => (e?.ref as HTMLInputElement)?.id === childNameRef.current?.id,
)
: undefined

const childName = watch(childNamePath) as unknown as string

const allChildren = useMemo<string[]>(() => {
Expand Down Expand Up @@ -322,33 +317,34 @@ const ChildrenBody = ({
<FormLabel gridArea="formlabel">Child</FormLabel>
<Flex align="stretch" alignItems="stretch" justify="space-between">
<Box flexGrow={10}>
<FormControl key={field.id} isRequired isInvalid={!!childNameError}>
<FormControl
key={field.id}
isRequired
isInvalid={!!error?.[CHILD_NAME_INDEX]}
>
<Controller
control={control}
name={childNamePath}
rules={validationRules}
rules={{
required: REQUIRED_ERROR,
}}
render={({
field: { value, onChange, onBlur, ref, ...rest },
}) => (
<SingleSelect
isRequired
{...rest}
placeholder={"Select your child's name"}
colorScheme={`theme-${colorTheme}`}
items={childNameValues}
value={value as unknown as string}
isDisabled={isSubmitting}
onChange={onChange}
ref={(e) => {
ref(e)
if (e) {
childNameRef.current = e
}
}}
/>
)}
/>
<FormErrorMessage>{childNameError?.message}</FormErrorMessage>
<FormErrorMessage>
{error?.[CHILD_NAME_INDEX]?.message}
</FormErrorMessage>
</FormControl>
</Box>
<IconButton
Expand Down Expand Up @@ -406,7 +402,7 @@ const ChildrenBody = ({
{MYINFO_ATTRIBUTE_MAP[subField].description}
</FormLabel>
<ChakraInput
{...register(fieldPath, validationRules)}
{...register(fieldPath, childrenValidationRules)}
value={value}
/>
<FormErrorMessage>
Expand All @@ -432,7 +428,7 @@ const ChildrenBody = ({
<Controller
control={control}
name={fieldPath}
rules={validationRules}
rules={childrenValidationRules}
render={({
field: { value, onChange, onBlur, ...rest },
}) => (
Expand Down Expand Up @@ -467,7 +463,7 @@ const ChildrenBody = ({
<Controller
control={control}
name={fieldPath}
rules={validationRules}
rules={childrenValidationRules}
render={({ field: { value, onChange, ...rest } }) => (
<DatePicker
{...rest}
Expand Down

0 comments on commit b23dce1

Please sign in to comment.