@@ -25,19 +25,21 @@ interface FormHelpers {
25
25
helperText ?: ReactNode
26
26
}
27
27
28
+ // backendErrorName can be used if the backend names a field differently than the frontend does
28
29
export const getFormHelpers =
29
- < T > ( form : FormikContextType < T > , formErrors ?: FormikErrors < T > ) =>
30
- ( name : keyof T , HelperText : ReactNode = "" ) : FormHelpers => {
30
+ < T > ( form : FormikContextType < T > , apiValidationErrors ?: FormikErrors < T > ) =>
31
+ ( name : keyof T , HelperText : ReactNode = "" , backendErrorName ?: string ) : FormHelpers => {
31
32
if ( typeof name !== "string" ) {
32
33
throw new Error ( `name must be type of string, instead received '${ typeof name } '` )
33
34
}
35
+ const apiErrorName = backendErrorName ?? name
34
36
35
37
// getIn is a util function from Formik that gets at any depth of nesting
36
38
// and is necessary for the types to work
37
39
const touched = getIn ( form . touched , name )
38
- const apiError = getIn ( formErrors , name )
39
- const validationError = getIn ( form . errors , name )
40
- const error = apiError ?? validationError
40
+ const apiError = getIn ( apiValidationErrors , apiErrorName )
41
+ const frontendError = getIn ( form . errors , name )
42
+ const error = apiError ?? frontendError
41
43
return {
42
44
...form . getFieldProps ( name ) ,
43
45
id : name ,
@@ -49,7 +51,7 @@ export const getFormHelpers =
49
51
export const getFormHelpersWithError = < T > (
50
52
form : FormikContextType < T > ,
51
53
error ?: Error | unknown ,
52
- ) : ( ( name : keyof T , HelperText ?: ReactNode ) => FormHelpers ) => {
54
+ ) : ( ( name : keyof T , HelperText ?: ReactNode , errorName ?: string ) => FormHelpers ) => {
53
55
const apiValidationErrors =
54
56
isApiError ( error ) && hasApiFieldErrors ( error )
55
57
? ( mapApiErrorToFieldErrors ( error . response . data ) as FormikErrors < T > )
0 commit comments