Skip to content

Commit

Permalink
fix: stop revalidating with stale values (#8193)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zangetsu101 authored Dec 24, 2024
1 parent e290682 commit 15efb5f
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions packages/client/src/components/form/FormFieldGenerator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ type GeneratedInputFieldProps = {
disabled?: boolean
onUploadingStateChanged?: (isUploading: boolean) => void
requiredErrorMessage?: MessageDescriptor
setFieldTouched: (name: string, isTouched?: boolean) => void
setFieldTouched: FormikProps<IFormSectionData>['setFieldTouched']
} & Omit<IDispatchProps, 'writeDeclaration'>

const GeneratedInputField = React.memo<GeneratedInputFieldProps>(
Expand Down Expand Up @@ -274,8 +274,18 @@ const GeneratedInputField = React.memo<GeneratedInputFieldProps>(
extraValue={fieldDefinition.extraValue || ''}
hideOnEmptyOption={fieldDefinition.hideOnEmptyOption}
onComplete={(files: IFileValue[]) => {
/*
* calling both setFieldTouched and setFieldValue causes the validate
* function to be called twice, once with the stale values (due to
* setFieldTouched) and the other with the updated values (due to
* setFieldValue). So if setFieldTouched is called after
* setFieldValue then wrong validations are shown due to the stale
* values. We can prevent that by supplying shouldRevalidate =
* false to the setFieldTouch function or calling it before
* calling setFieldValue.
*/
setFieldTouched(fieldDefinition.name, true, false)
setFieldValue(fieldDefinition.name, files)
setFieldTouched && setFieldTouched(fieldDefinition.name, true)
}}
compressImagesToSizeMB={fieldDefinition.compressImagesToSizeMB}
maxSizeMB={fieldDefinition.maxSizeMB}
Expand All @@ -296,7 +306,7 @@ const GeneratedInputField = React.memo<GeneratedInputFieldProps>(
files={value as IAttachmentValue}
error={error}
onComplete={(file) => {
setFieldTouched && setFieldTouched(fieldDefinition.name, true)
setFieldTouched(fieldDefinition.name, true, false)
setFieldValue(fieldDefinition.name, file)
}}
onUploadingStateChanged={onUploadingStateChanged}
Expand Down

0 comments on commit 15efb5f

Please sign in to comment.