Skip to content

Commit 6694d35

Browse files
author
Georgi2704
committed
Fix validation error not resetting when form field has change
1 parent e507161 commit 6694d35

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

frontend/packages/pydantic-forms/src/core/PydanticFormHandler.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import { getHashForArray } from '@/utils';
1111
import { ReactHookForm } from './ReactHookForm';
1212

1313
export const PydanticFormHandler = ({
14-
formKey,
15-
onCancel,
16-
onSuccess,
17-
title,
18-
}: PydanticFormHandlerProps) => {
14+
formKey,
15+
onCancel,
16+
onSuccess,
17+
title,
18+
}: PydanticFormHandlerProps) => {
1919
const config = useGetConfig();
2020
const [formStep, setStep] = useState<FieldValues>();
2121
const formStepsRef = useRef<FieldValues[]>([]);
@@ -68,6 +68,7 @@ export const PydanticFormHandler = ({
6868
isLoading,
6969
pydanticFormSchema,
7070
defaultValues,
71+
handleRemoveValidationError
7172
} = usePydanticForm(
7273
formKey,
7374
config,
@@ -117,6 +118,7 @@ export const PydanticFormHandler = ({
117118
onPrevious={onPrevious}
118119
pydanticFormSchema={pydanticFormSchema}
119120
title={title}
121+
handleRemoveValidationError={handleRemoveValidationError}
120122
/>
121123
</PydanticFormValidationErrorContext.Provider>
122124
);

frontend/packages/pydantic-forms/src/core/ReactHookForm.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* Here we define the outline of the form
77
*/
8-
import React from 'react';
8+
import React, {useEffect} from 'react';
99
import type { FieldValues } from 'react-hook-form';
1010
import { FormProvider, useForm } from 'react-hook-form';
1111

@@ -36,6 +36,7 @@ export interface ReactHookFormProps {
3636
onPrevious: () => void;
3737
pydanticFormSchema?: PydanticFormSchema;
3838
title?: string;
39+
handleRemoveValidationError: (location: string[]) => void;
3940
}
4041

4142
export const ReactHookForm = ({
@@ -51,6 +52,7 @@ export const ReactHookForm = ({
5152
onPrevious,
5253
pydanticFormSchema,
5354
title,
55+
handleRemoveValidationError
5456
}: ReactHookFormProps) => {
5557
const config = useGetConfig();
5658
const t = useTranslations('renderForm');
@@ -73,6 +75,13 @@ export const ReactHookForm = ({
7375
values: initialValues || defaultValues,
7476
});
7577

78+
useEffect(() => {
79+
const reactHookFormWatch = reactHookForm.watch((_, { name }) =>
80+
name && handleRemoveValidationError([name])
81+
);
82+
return reactHookFormWatch.unsubscribe;
83+
}, [handleRemoveValidationError, reactHookForm]);
84+
7685
if (apiError) {
7786
console.error('API Error:', apiError);
7887
return ErrorComponent;

frontend/packages/pydantic-forms/src/core/hooks/usePydanticForm.tsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,32 @@ export interface UsePydanticFormReturn {
2525
isSending: boolean;
2626
pydanticFormSchema?: PydanticFormSchema;
2727
defaultValues: FieldValues;
28+
handleRemoveValidationError: (location: string[]) => void;
29+
}
30+
31+
const removeValidationErrorByLoc = (
32+
validationErrors: PydanticFormValidationErrorDetails | null,
33+
locToRemove: (string | number)[]
34+
): PydanticFormValidationErrorDetails | null => {
35+
36+
if (!validationErrors) return null;
37+
38+
const newSource = validationErrors.source.filter(
39+
(err) => JSON.stringify(err.loc) !== JSON.stringify(locToRemove)
40+
);
41+
42+
const topKey = locToRemove[0]?.toString();
43+
const newMapped = { ...validationErrors.mapped };
44+
45+
if (topKey && newMapped[topKey]) {
46+
delete newMapped[topKey];
47+
}
48+
49+
return {
50+
...validationErrors,
51+
source: newSource,
52+
mapped: newMapped,
53+
};
2854
}
2955

3056
export function usePydanticForm(
@@ -37,6 +63,7 @@ export function usePydanticForm(
3763
response: PydanticFormSuccessResponse,
3864
) => void,
3965
formStep?: FieldValues,
66+
4067
): UsePydanticFormReturn {
4168
const emptyRawSchema: PydanticFormSchemaRawJson = useMemo(
4269
() => ({
@@ -131,6 +158,12 @@ export function usePydanticForm(
131158
// eslint-disable-next-line react-hooks/exhaustive-deps
132159
}, [apiResponse]);
133160

161+
const handleRemoveValidationError = (location: string[]) => {
162+
setValidationErrorsDetails((prev) =>
163+
removeValidationErrorByLoc(prev, location)
164+
);
165+
}
166+
134167
return {
135168
validationErrorsDetails,
136169
apiError,
@@ -140,5 +173,6 @@ export function usePydanticForm(
140173
pydanticFormSchema,
141174
defaultValues,
142175
isSending,
176+
handleRemoveValidationError
143177
};
144178
}

0 commit comments

Comments
 (0)