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

🐛 bug(validatingFields, trigger): handle all fields validation trigger #11624

Merged
merged 7 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 25 additions & 25 deletions app/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 3 additions & 7 deletions src/__tests__/useForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1874,12 +1874,6 @@ describe('useForm', () => {
target: { value: 'test1' },
});

expect(formState.validatingFields).toStrictEqual({ lastName: true });

await actComponent(async () => {
jest.advanceTimersByTime(1500);
});

expect(formState.validatingFields).toStrictEqual({ lastName: true });
expect(getFieldState('lastName').isValidating).toBe(true);

Expand Down Expand Up @@ -2008,7 +2002,9 @@ describe('useForm', () => {
unregister('firstName');
jest.runAllTimers();
});
expect(formState.validatingFields).toEqual({});
expect(formState.validatingFields).toEqual({
firstName: false,
});
});

it('should update defaultValues async', async () => {
Expand Down
29 changes: 14 additions & 15 deletions src/logic/createFormControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,11 @@ export function createFormControl<
}
};

const _updateIsValidating = (names: string[], isValidating?: boolean) => {
const _updateIsValidating = (names?: string[], isValidating?: boolean) => {
if (_proxyFormState.isValidating || _proxyFormState.validatingFields) {
names.forEach((name) =>
set(_formState.validatingFields, name, !!isValidating),
(names || Array.from(_names.mount)).forEach(
(name) =>
name && set(_formState.validatingFields, name, !!isValidating),
);
_formState.isValidating = Object.values(_formState.validatingFields).some(
(val) => val,
Expand Down Expand Up @@ -396,14 +397,11 @@ export function createFormControl<

_subjects.state.next(updatedFormState);
}

_updateIsValidating(
Object.keys(_formState.validatingFields).filter((key) => key === name),
);
};

const _executeSchema = async (name?: InternalFieldName[]) =>
_options.resolver!(
const _executeSchema = async (name?: InternalFieldName[]) => {
_updateIsValidating(name, true);
const result = await _options.resolver!(
_formValues as TFieldValues,
_options.context,
getResolverOptions(
Expand All @@ -413,6 +411,9 @@ export function createFormControl<
_options.shouldUseNativeValidation,
),
);
_updateIsValidating(name);
return result;
};

const executeSchemaAndUpdateState = async (names?: InternalFieldName[]) => {
const { errors } = await _executeSchema(names);
Expand Down Expand Up @@ -448,13 +449,15 @@ export function createFormControl<

if (_f) {
const isFieldArrayRoot = _names.array.has(_f.name);
_updateIsValidating([name], true);
const fieldError = await validateField(
field,
_formValues,
shouldDisplayAllAssociatedErrors,
_options.shouldUseNativeValidation && !shouldOnlyCheckValid,
isFieldArrayRoot,
);
_updateIsValidating([name]);

if (fieldError[_f.name]) {
context.valid = false;
Expand Down Expand Up @@ -744,8 +747,6 @@ export function createFormControl<

!isBlurEvent && watched && _subjects.state.next({ ..._formState });

_updateIsValidating([name], true);

if (_options.resolver) {
const { errors } = await _executeSchema([name]);

Expand All @@ -769,6 +770,7 @@ export function createFormControl<
isValid = isEmptyObject(errors);
}
} else {
_updateIsValidating([name], true);
error = (
await validateField(
field,
Expand All @@ -777,6 +779,7 @@ export function createFormControl<
_options.shouldUseNativeValidation,
)
)[name];
_updateIsValidating([name]);

_updateIsFieldValueUpdated(fieldValue);

Expand Down Expand Up @@ -814,8 +817,6 @@ export function createFormControl<
let validationResult;
const fieldNames = convertToArrayPayload(name) as InternalFieldName[];

_updateIsValidating(fieldNames, true);

if (_options.resolver) {
const errors = await executeSchemaAndUpdateState(
isUndefined(name) ? name : fieldNames,
Expand Down Expand Up @@ -850,8 +851,6 @@ export function createFormControl<
errors: _formState.errors,
});

_updateIsValidating(fieldNames);

options.shouldFocus &&
!validationResult &&
iterateFieldsByAction(
Expand Down