Skip to content

Commit d9ac312

Browse files
author
Tim Roes
authored
🪟 🐛 Correctly revalidate form when validationSchema changes (#15109)
* Correctly revalidate form when validationSchema changes * Remove old validation logic
1 parent e28038e commit d9ac312

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

airbyte-webapp/src/views/Connector/ServiceForm/ServiceForm.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,21 @@ const PatchInitialValuesWithWidgetConfig: React.FC<{
6969
return null;
7070
};
7171

72+
/**
73+
* Formik does not revalidate the form in case the validationSchema it's using changes.
74+
* This component just forces a revalidation of the form whenever the validation schema changes.
75+
*/
76+
const RevalidateOnValidationSchemaChange: React.FC<{ validationSchema: unknown }> = ({ validationSchema }) => {
77+
// The validationSchema is passed into this component instead of pulled from the FormikContext, since
78+
// due to https://github.com/jaredpalmer/formik/issues/2092 the validationSchema from the formik context will
79+
// always be undefined.
80+
const { validateForm } = useFormikContext();
81+
useEffect(() => {
82+
validateForm();
83+
}, [validateForm, validationSchema]);
84+
return null;
85+
};
86+
7287
/**
7388
* A component that will observe whenever the serviceType (selected connector)
7489
* changes and set the name of the connector to match the connector definition name.
@@ -252,6 +267,7 @@ const ServiceForm: React.FC<ServiceFormProps> = (props) => {
252267
isLoadingSchema={props.isLoading}
253268
>
254269
{!props.isEditMode && <SetDefaultName />}
270+
<RevalidateOnValidationSchemaChange validationSchema={validationSchema} />
255271
<FormikPatch />
256272
<FormChangeTracker changed={dirty} formId={formId} />
257273
<PatchInitialValuesWithWidgetConfig schema={jsonSchema} initialValues={initialValues} />

airbyte-webapp/src/views/Connector/ServiceForm/useBuildForm.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,7 @@ export const useConstructValidationSchema = (jsonSchema: JSONSchema7, uiWidgetsI
129129
useMemo(() => buildYupFormForJsonSchema(jsonSchema, uiWidgetsInfo), [uiWidgetsInfo, jsonSchema]);
130130

131131
export const usePatchFormik = (): void => {
132-
const { setFieldTouched, isSubmitting, isValidating, validationSchema, validateForm, errors } = useFormikContext();
133-
// Formik doesn't validate values again, when validationSchema was changed on the fly.
134-
useEffect(() => {
135-
validateForm();
136-
}, [validateForm, validationSchema]);
132+
const { setFieldTouched, isSubmitting, isValidating, errors } = useFormikContext();
137133

138134
/* Fixes issue https://github.com/airbytehq/airbyte/issues/1978
139135
Problem described here https://github.com/formium/formik/issues/445

0 commit comments

Comments
 (0)