perf: skips field validations until the form is submitted (#10580)
Field validations can be expensive, especially custom validations that are async or highly complex. This can lead to slow form state response times when generating form state for many such fields. Ideally, we only run validations on fields whose values have changed. This is not possible, however, because field validation functions might reference _other_ field values with their args, and there is no good way of detecting exactly which fields should run in this case. The next best thing here is to only run validations _after the form has been submitted_, and then every `onChange` event thereafter until a successful submit has taken place. This is an elegant solution because we currently don't _render_ field errors until submission anyway. This change will significantly speed up form state response times, at least until the form has been submitted. From then on, all field validations will run regardless, just as they do now. If custom validations continue to slow down form state response times, there is a new `event` arg introduced in #10738 that can be used to control whether heavy operations occur on change or on submit. Related: #10638
This commit is contained in:
@@ -108,7 +108,7 @@ export function EditForm({ submitted }: EditFormProps) {
|
||||
)
|
||||
|
||||
const onChange: NonNullable<FormProps['onChange']>[0] = useCallback(
|
||||
async ({ formState: prevFormState }) => {
|
||||
async ({ formState: prevFormState, submitted }) => {
|
||||
const controller = handleAbortRef(abortOnChangeRef)
|
||||
|
||||
const docPreferences = await getDocPreferences()
|
||||
@@ -121,6 +121,7 @@ export function EditForm({ submitted }: EditFormProps) {
|
||||
operation: 'create',
|
||||
schemaPath,
|
||||
signal: controller.signal,
|
||||
skipValidation: !submitted,
|
||||
})
|
||||
|
||||
abortOnChangeRef.current = null
|
||||
|
||||
@@ -216,6 +216,7 @@ export function FormsManagerProvider({ children }: FormsManagerProps) {
|
||||
operation: 'create',
|
||||
renderAllFields: true,
|
||||
schemaPath: collectionSlug,
|
||||
skipValidation: true,
|
||||
})
|
||||
initialStateRef.current = formStateWithoutFiles
|
||||
setHasInitializedState(true)
|
||||
|
||||
Reference in New Issue
Block a user