final-form / react-final-form

🏁 High performance subscription-based form state management for React
https://final-form.org/react
MIT License
7.39k stars 482 forks source link

Error messages on nested object values #1007

Closed Haaxor1689 closed 1 year ago

Haaxor1689 commented 2 years ago

Are you submitting a bug report or a feature request?

Bug report

What is the current behavior?

If the value of key I try to set error on is an object, the error comes out as an object as well. So let's say I return from validate { field: 'Error' }. In the errors it will be {"field":{"0":"E","1":"r","2":"r","3":"o","4":"r"}} instead.

What is the expected behavior?

If I set the error of object type attribute, I'd like it to remain a string.

Sandbox Link

https://codesandbox.io/s/elastic-tom-rtk8s8?file=/src/App.js

What's your environment?

See code sandbox

Other information

829 describes similar issue but with IE and slightly different use case

gertdreyer commented 1 year ago

Check your validator function return. If you change the return to {errors: { field: "Error" }} it works as expected

validate={(v) =>
        v.field?.a?.length !== v.field?.b?.length
          ? {errors: { field: "Error" }}
          : undefined
      }