react-admin icon indicating copy to clipboard operation
react-admin copied to clipboard

Global validation not firing after submit with ArrayInput

Open nathan-britten opened this issue 2 years ago • 2 comments

What you were expecting: I have a form with the mode and reValidateMode set to onBlur, when I submit the form the input validation should still fire onBlur.

What happened instead: Once the form has been resubmitted onBlur validation no longer works. The form is only revalidation onSubmit

Steps to reproduce:

  1. Go to the Sandbox https://codesandbox.io/s/hardcore-edison-ntjjfj?file=/src/posts/PostEdit.tsx:2932-2946
  2. Enter a word in the top title
  3. Click save
  4. Enter text into the errored inputs
  5. The error messages still remain

Related code: https://codesandbox.io/s/hardcore-edison-ntjjfj?file=/src/posts/PostEdit.tsx:2932-2946

Other information:

Environment

  • React-admin version: 4.0.1
  • Last version that did not exhibit the issue (if applicable):
  • React version: 17.0.2
  • Browser: Chrome
  • Stack trace (in case of a JS error):

nathan-britten avatar Apr 27 '22 15:04 nathan-britten

Hi! Thank you for submitting this. I reproduced the issue thanks to your codesandbox. However, I noted that this issue only happens inside an ArrayInput component. It does not happen when you use plain TextInput for example. There might be something wrong in the way we handle input identifiers there, I'll need to investigate.

slax57 avatar Apr 28 '22 10:04 slax57

Probably linked to: https://github.com/marmelab/react-admin/issues/7591#issuecomment-1115018804

slax57 avatar May 02 '22 16:05 slax57

This issue still happens even after #8080.

I took some time to do some testing around, and now I'm convinced this problem does not come from react-admin but rather from react-hook-form. I opened an issue on their repo: https://github.com/react-hook-form/react-hook-form/issues/8927

Hopefully they will be able to fix it or tell us what we do wrong.

slax57 avatar Aug 26 '22 16:08 slax57

Okay so @bluebill1049 answered and it appears the format of our errors object is wrong in case of array fields. We should have

{
  test: [{firstName: {....}}]
}

rather than

{"test.0.firstName":{"type":"required","message":"firstName is required."}

This can probably be fixed inside getSimpleValidationResolver.ts

slax57 avatar Aug 29 '22 07:08 slax57