final-form / react-final-form

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

Array field-level validation #160

Closed bogdansoare closed 6 years ago

bogdansoare commented 6 years ago

I've tried implementing array field-level validation but I get the following error when I try to delete the last item in the array Cannot set a numeric property on an object.

I would like to validate the FieldArray and also each subfield in the array.

Link to the codesandox .

As a side note I've also took a look at https://github.com/final-form/final-form/issues/35 but the proposed solution with record-level validation isn't working with the latest versions of the library https://codesandbox.io/s/2pnv1x052y Also I find record-level validation much more repetitive and kind of unusable in a real app compared to having some functions for field-level validation

kavink commented 6 years ago

97 has some tips.

bogdansoare commented 6 years ago

@kavink thanks for the link, but that still doesn't solve my issue.

@erikras any thoughts on this?

erikras commented 6 years ago

Well reported, @bogdansoare. Will investigate... 🔍

djeeg commented 6 years ago

Upgrading from redux-form, I tried how its referenced here https://redux-form.com/6.7.0/examples/fieldarrays/, ie change the array to an object and use _error property.

Unfortuately that didnt work

erikras commented 6 years ago

@djeeg Try this:

import { ARRAY_ERROR } from 'final-form'
...
errors.myArray = []
errors.myArray[ARRAY_ERROR] = 'Error for the whole array'
djeeg commented 6 years ago

Ahh I just found your latest PR with ARRAY_ERROR https://github.com/final-form/final-form/pull/92

Trying it here: https://codesandbox.io/s/kmr4zj22z5

Though still get it to trigger an error

EDIT

Got it, thanks

bogdansoare commented 6 years ago

@erikras did you have time to check array field-level validation ?

erikras commented 6 years ago

No, but this is high on my list of examples to create, so it will get done soon.

bogdansoare commented 6 years ago

@erikras sorry to bother you again, any progress on this?

erikras commented 6 years ago

Thanks for the nudge, @bogdansoare. 👍

erikras commented 6 years ago

Published fix in react-final-form-arrays@1.0.5.

Original sandbox with updated deps:

Edit 🏁 React Final Form - Array field-level validation

bogdansoare commented 6 years ago

awesome, thank you very much ❤️

lock[bot] commented 5 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.