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: Array error does not update on a change if any of its fields have errors #10139

Closed
1 task done
kylemclean opened this issue Mar 20, 2023 · 10 comments
Closed
1 task done
Labels
status: under investigation aware of this issue and pending for investigation

Comments

@kylemclean
Copy link
Contributor

kylemclean commented Mar 20, 2023

Version Number

7.43.7

Codesandbox/Expo snack

https://codesandbox.io/s/wispy-surf-znf62c?file=/src/App.tsx

Steps to reproduce

  1. Go to CodeSandbox.

  2. Click "Submit" to start displaying validation errors on change.
    Screenshot from 2023-03-31 22-25-26

  3. Click "Add" button to add an element to the array, causing the array length to be too long, and the error "Form error: Array must contain at most 3 element(s)" to correctly appear.
    Screenshot from 2023-03-31 22-25-39

  4. Click "Remove" next to the newly-added element to remove it, causing the array length to be valid, thus causing the error to correctly disappear.
    Screenshot from 2023-03-31 22-25-44

  5. Delete all of the text in one of the existing array elements, making it invalid and causing an error to correctly appear.
    Screenshot from 2023-03-31 22-25-56

  6. Click the "Add" button again to add another element to the array.
    INCORRECT BEHAVIOR: even though the array length is no longer valid, the error from before, "Form error: Array must contain at most 3 element(s)", is NOT being displayed.
    Screenshot from 2023-03-31 22-26-14

  7. Click the "Submit" button, which causes the expected error "Form error: Array must contain at most 3 element(s)" to correctly display.
    Screenshot from 2023-03-31 22-26-28

  8. Click "Remove" next to the newly-added element, causing the array length to be valid.
    INCORRECT BEHAVIOR: even though the array length is now valid, the error "Form error: Array must contain at most 3 element(s)" is still incorrectly being displayed.
    Screenshot from 2023-03-31 22-26-40

  9. Click the "Submit" button, which causes the incorrect error "Form error: Array must contain at most 3 element(s)" to disappear.
    Screenshot from 2023-03-31 22-26-46

Expected behaviour

On step 6, clicking the "Add" button should immediately display the error "Form error: Array must contain at most 3 element(s)" instead of requiring the form to be submitted to display it.

On step 8, clicking the "Remove" button should immediately hide the error "Form error: Array must contain at most 3 element(s)" instead of requiring the form to be submitted to hide it.

What browsers are you seeing the problem on?

Firefox, Chrome

Relevant log output

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@bluebill1049
Copy link
Member

Array error will not get updated by onChange as hook form validation based on field level, each user action (append, prepend and etc.) do update the field array. You can always fallback to trigger to trigger field array level validation.

@bluebill1049 bluebill1049 added the question Further information is requested label Mar 20, 2023
@kylemclean
Copy link
Contributor Author

The issue I am describing is that appends and removes on the array are sometimes not revalidating the array. This appears to be a bug.

@bluebill1049
Copy link
Member

Please do your own debugging: https://react-hook-form.com/api/useform/

resolver: async (data, context, options) => {
  // you can debug your validation schema here
  console.log('formData', data)
  console.log('validation result', await anyResolver(schema)(data, context, options))
  return anyResolver(schema)(data, context, options)
},

see if the validation schema return the correct output. validation will always gets executed after each action: https://github.com/react-hook-form/react-hook-form/blob/master/src/useFieldArray.ts#L323-L361

@kylemclean
Copy link
Contributor Author

I have now confirmed using the debug logging that the validation is executed and returns the correct output on steps 6 and 8, but the displayed error does not change until the form is submitted. The CodeSandbox link has been updated to include the debug logging.

The problem seems to be with line 329 in useFieldArray.ts. On steps 6 and 8, both existingError and error are truthy, causing the update to control._formState.errors to be skipped, and thus the error state is wrong.

@bluebill1049
Copy link
Member

The problem seems to be with line 329 in useFieldArray.ts. On steps 6 and 8, both existingError and error are truthy, causing the update to control._formState.errors to be skipped, and thus the error state is wrong.

thanks for looking close into this issue, could you debug further on this since you are already deep into the issue, and find out the root cause? If it's possible to send over a PR would be even greater.

@kylemclean
Copy link
Contributor Author

I will see what I can do.

@bluebill1049
Copy link
Member

bluebill1049 commented Mar 30, 2023

Screenshot 2023-03-31 at 8 13 55 am

just following up on this issue again, step 6 doesn't return any errors with 7.44.7. I will have to revert the fix for now as it introduced a regression.

@abhinav2712

This comment was marked as spam.

@bluebill1049
Copy link
Member

hey @kylemclean could you share more detail on the origin issue? I test above seems to work fine. I will look into it this weekend.

@kylemclean
Copy link
Contributor Author

@bluebill1049
I have updated the issue with some screenshots that should more clearly illustrate the problem.

Please also see my comment on #10207 (comment), which might help demonstrate why my PR #10150 resulted in #10207 appearing.

bluebill1049 added a commit that referenced this issue Apr 1, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 1, 2023
kylemclean pushed a commit to kylemclean/react-hook-form that referenced this issue Feb 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
status: under investigation aware of this issue and pending for investigation
Projects
None yet
Development

No branches or pull requests

3 participants