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

issue: setValue an empty array to a nested field doesn't trigger validation when shouldValidate is true #10862

Closed
1 task done
novakdan opened this issue Aug 29, 2023 · 3 comments · May be fixed by #11336
Closed
1 task done
Assignees

Comments

@novakdan
Copy link

Version Number

7.43.9

Codesandbox/Expo snack

https://codesandbox.io/s/dark-wave-y2zj85

Steps to reproduce

I have a nested field and a custom component hooked to RHF via useController. The custom component has string[] as its value type.

I need to set the value to []. These are two ways that I tried.

a)

setValue("item1.positions", [{ activities: [] }], {
   shouldValidate: true
});

b)

setValue("item2.positions.0.activities", [], {
   shouldValidate: true
});

a) doesn't work well,
b) works as expected

There are two issues:

  1. useController doesn't pick up on the state change because it has exact internally set to true by default without the option to set it. That is why the component doesn't even re-render upon the setValue call => I have found a solution to this - I'm using useWatch with exact set to false.
  2. error is not set. It is not just that useController doesn't cause the component to re-render (as I stated in my 1. point), the error isn't anywhere. I can even use useFormState({ exact: false }) and it still doesn't show.

I have been debugging the 2. issue for a long time and I may have found the reason in the setValues function.

  const setValues = <
    T extends InternalFieldName,
    K extends SetFieldValue<TFieldValues>,
    U extends SetValueConfig,
  >(
    name: T,
    value: K,
    options: U,
  ) => {
    for (const fieldKey in value) {
      const fieldValue = value[fieldKey];
      const fieldName = `${name}.${fieldKey}`;
      const field = get(_fields, fieldName);

      (_names.array.has(name) ||
        !isPrimitive(fieldValue) ||
        (field && !field._f)) &&
      !isDateObject(fieldValue)
        ? setValues(fieldName, fieldValue, options)
        : setFieldValue(fieldName, fieldValue, options);
    }
  };

As it recursively goes through the field name, to parse through the name and use the correct value type, it encounters [] as the value and it never calls setFieldValue, because [] is not a primitive value. If the value to set was a primitive, this process wouldn't have been stopped in its tracks and it would have updated the value and formState. It doesn't register the error. I believe it would behave the same if I passed an empty object (to a different field of course) - as it isn't primitive either.

I have not found a solution for this issue. The only workaround was to call trigger immediately after setting the value. That works but I would like to avoid that.

Expected behaviour

Empty array [] should be a valid value and it should correctly trigger validation when shouldValidate is set to true.

What browsers are you seeing the problem on?

Chrome

Relevant log output

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@bluebill1049 bluebill1049 added the enhancement New feature or request label Aug 29, 2023
@sungh0lim
Copy link

Hello, @bluebill1049. May i handle this issue? Please assign to me. I'll create PR.

@bluebill1049
Copy link
Member

thank you @sungh0lim 🙏

@bluebill1049
Copy link
Member

I don't think we will support this usage as this would further complicate the setValue logic, what i would recommend to use setValue within a loop instead and update your specific field value.

@bluebill1049 bluebill1049 closed this as not planned Won't fix, can't repro, duplicate, stale Jan 13, 2024
@bluebill1049 bluebill1049 added work as designed and removed enhancement New feature or request labels Jan 13, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants