Skip to content

Commit

Permalink
🐛 bug(validatingFields, trigger): handle all fields validation trigger (
Browse files Browse the repository at this point in the history
react-hook-form#11624)

* WIP 🐛 bug(validatingFields, trigger): handle all fields validation trigger

* 🔧 chore: update lock file

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

* 🔧 chore: remove todo comment

* fix react-hook-form#11621 issue with undefined name as field name

* fix lint

* revert change on trigger

---------

Co-authored-by: bill <bluebill1049@hotmail.com>
  • Loading branch information
2 people authored and rafaelcalhau committed May 5, 2024
1 parent e96a6c9 commit a2f08c7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 47 deletions.
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
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
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 @@ -748,8 +751,6 @@ export function createFormControl<

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

_updateIsValidating([name], true);

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

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

_updateIsFieldValueUpdated(fieldValue);

Expand Down Expand Up @@ -818,8 +821,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 @@ -854,8 +855,6 @@ export function createFormControl<
errors: _formState.errors,
});

_updateIsValidating(fieldNames);

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

0 comments on commit a2f08c7

Please sign in to comment.