@@ -37,13 +37,12 @@ interface FormHelpers {
37
37
helperText ?: ReactNode
38
38
}
39
39
40
- // backendErrorName can be used if the backend names a field differently than the frontend does
41
40
export const getFormHelpers =
42
41
< T > ( form : FormikContextType < T > , error ?: Error | unknown ) =>
43
42
(
44
43
name : keyof T ,
45
44
HelperText : ReactNode = "" ,
46
- backendErrorName ?: string ,
45
+ friendlyLabel ?: string ,
47
46
) : FormHelpers => {
48
47
const apiValidationErrors =
49
48
isApiError ( error ) && hasApiFieldErrors ( error )
@@ -54,19 +53,19 @@ export const getFormHelpers =
54
53
`name must be type of string, instead received '${ typeof name } '` ,
55
54
)
56
55
}
57
- const apiErrorName = backendErrorName ?? name
58
56
59
57
// getIn is a util function from Formik that gets at any depth of nesting
60
58
// and is necessary for the types to work
61
59
const touched = getIn ( form . touched , name )
62
- const apiError = getIn ( apiValidationErrors , apiErrorName )
60
+ const apiError = getIn ( apiValidationErrors , name )
63
61
const frontendError = getIn ( form . errors , name )
64
62
const returnError = apiError ?? frontendError
63
+ const friendlyError = friendlyLabel && returnError ? returnError . replace ( name , friendlyLabel ) : returnError
65
64
return {
66
65
...form . getFieldProps ( name ) ,
67
66
id : name ,
68
- error : touched && Boolean ( returnError ) ,
69
- helperText : touched ? returnError || HelperText : HelperText ,
67
+ error : touched && Boolean ( friendlyError ) ,
68
+ helperText : touched ? friendlyError || HelperText : HelperText ,
70
69
}
71
70
}
72
71
0 commit comments