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

Server-Side validation only works once if returning errors object with an unexisting field

Open newfylox opened this issue 2 years ago • 3 comments

I know in documentation it says that the errors object must match the fields of the form, but it was to tell that if not, the errors won't show in the form.

Indeed, in V3, providing an errors object with dynamic fields (fields that doesn't exist in the form but come from the API for example) would only show errors for exact field names and the form would work each time we hit the "save" button.

But in V4, it will only work once (when clicking "save" button) but after it kind of crashes and we can't click on save button anymore.

newfylox avatar Jul 12 '22 18:07 newfylox

Reproduced, thanks!

slax57 avatar Aug 22 '22 16:08 slax57

Hello, I have exactly the same problem with admin v4. Did you find a temporary solution?

bbelletoile avatar Aug 25 '22 15:08 bbelletoile

Until the bug is fixed, I have 2 solutions

  • fix your server API endpoint to return exactly what the frontend needs for fields (not always easy)
  • on the frontend side, before returning the error object, filter the object by removing/adding keys so it only contains keys that fit the submitted form (that's what I did. For example my server returned { first_name: "First", last_name: "Last", other_key: "value" } as object and before returning that object, I removed keys that didn't fit with the form like this { first_name: "First", last_name: "Last" })

newfylox avatar Aug 26 '22 16:08 newfylox

It seems that it's related to react-hook-form and not react-admin. But I remember that in one of your previous versions, your code was kind of sanitizing useless/unrelated fields.

I started an issue (that turned to discussion) with react-hook-form team and I thought I could share it here and probably asking to close this issue.

https://github.com/react-hook-form/react-hook-form/discussions/9906

But maybe you have a good idea or code example to implement a generic setErrors that uses setError behind the scenes and can ignore unrelated form fields so we can use it.

Thanks

newfylox avatar Feb 15 '23 18:02 newfylox

@newfylox Thank you for taking the time to push the analysis further, and for sharing your results so far!

I agree that this seems to be a RHF limitation (for now), and hence I'll close this issue.

Regarding the last part of your answer however, I'm not sure I caught exactly what you are asking for. If that helps, the setError callback is called here, which is called from the submit callback of useAugmentedForm here. If you need to implement some logic to filter the fields on which you will call setError, you can probably provide your own SaveContext, which would wrap the return value (i.e. the errors) of the original save callback, and filter out all the non-registered fields. You may be able to get the registered fields list by calling getValues with no parameter.

slax57 avatar Feb 16 '23 09:02 slax57