Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🪟 🐛 Correctly revalidate form when validationSchema changes #15109

Merged
merged 3 commits into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions airbyte-webapp/src/views/Connector/ServiceForm/ServiceForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,21 @@ const PatchInitialValuesWithWidgetConfig: React.FC<{
return null;
};

/**
* Formik does not revalidate the form in case the validationSchema it's using changes.
* This component just forces a revalidation of the form whenever the validation schema changes.
*/
const RevalidateOnValidationSchemaChange: React.FC<{ validationSchema: unknown }> = ({ validationSchema }) => {
// The validationSchema is passed into this component instead of pulled from the FormikContext, since
// due to https://github.com/jaredpalmer/formik/issues/2092 the validationSchema from the formik context will
// always be undefined.
const { validateForm } = useFormikContext();
useEffect(() => {
validateForm();
}, [validateForm, validationSchema]);
return null;
};

/**
* A component that will observe whenever the serviceType (selected connector)
* changes and set the name of the connector to match the connector definition name.
Expand Down Expand Up @@ -247,6 +262,7 @@ const ServiceForm: React.FC<ServiceFormProps> = (props) => {
isLoadingSchema={props.isLoading}
>
{!props.isEditMode && <SetDefaultName />}
<RevalidateOnValidationSchemaChange validationSchema={validationSchema} />
<FormikPatch />
<FormChangeTracker changed={dirty} formId={formId} />
<PatchInitialValuesWithWidgetConfig schema={jsonSchema} initialValues={initialValues} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,7 @@ const useConstructValidationSchema = (jsonSchema: JSONSchema7, uiWidgetsInfo: Wi
useMemo(() => buildYupFormForJsonSchema(jsonSchema, uiWidgetsInfo), [uiWidgetsInfo, jsonSchema]);

const usePatchFormik = (): void => {
const { setFieldTouched, isSubmitting, isValidating, validationSchema, validateForm, errors } = useFormikContext();
// Formik doesn't validate values again, when validationSchema was changed on the fly.
useEffect(() => {
validateForm();
}, [validateForm, validationSchema]);
const { setFieldTouched, isSubmitting, isValidating, errors } = useFormikContext();

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