Skip to content

Commit

Permalink
Fix useFormGroup doesn't return validation errors with react-hook-f…
Browse files Browse the repository at this point in the history
…orm 7.53.0

(cherry picked from commit 303fdc3)
  • Loading branch information
slax57 authored and Michel Paiva committed Aug 28, 2024
1 parent 090524c commit 7745d52
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions packages/ra-core/src/form/useFormGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,14 @@ export const useFormGroup = (name: string): FormGroupState => {
const { dirtyFields, touchedFields, validatingFields, errors } =
useFormState();

// dirtyFields, touchedFields and validatingFields are objects with keys being the field names
// dirtyFields, touchedFields, validatingFields and errors are objects with keys being the field names
// Ex: { title: true }
// However, they are not correctly serialized when using JSON.stringify
// To avoid our effects to not be triggered when they should, we extract the keys and use that as a dependency
const dirtyFieldsNames = Object.keys(dirtyFields);
const touchedFieldsNames = Object.keys(touchedFields);
const validatingFieldsNames = Object.keys(validatingFields);
const errorsNames = Object.keys(errors);

const formGroups = useFormGroups();
const [state, setState] = useState<FormGroupState>({
Expand Down Expand Up @@ -121,7 +122,8 @@ export const useFormGroup = (name: string): FormGroupState => {
[
// eslint-disable-next-line react-hooks/exhaustive-deps
JSON.stringify(dirtyFieldsNames),
errors,
// eslint-disable-next-line react-hooks/exhaustive-deps
JSON.stringify(errorsNames),
// eslint-disable-next-line react-hooks/exhaustive-deps
JSON.stringify(touchedFieldsNames),
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down

0 comments on commit 7745d52

Please sign in to comment.