Removing an entry from an array causes the array to receive invalid data
form.removeValue (with the correct index) but then when I check the form.state.values.roles there is a weird entry of whatever the last entry of the initial array was but now with just the name property of the whole Role object, which in turn causes my zod validation to fail. I have a stackblitz where this odd behaviour can be reproduced: https://stackblitz.com/edit/tanstack-form-9vub96pu?file=src%2Findex.tsx
Assuming when I'm editing the user which has both
Admin and User:1. I click
Admin to remove it1. In the breakpoint I have on the
onChange I correctly see that event.target.checked is false and the else branch is executed1. When logging
form.state.values at this point I see the correct data1. It correctly finds the
id of the role to remove1.
field.removeValue(roleIdx) seems to be called with the correct value1. The form state now results in
1. Click the Submit button
This then trips up Zod which says that
roles[1].external is required.What I find particularly notable is that as far as I can tell this problem is only happening when editing an existing user, but when I log the whole
form.state.values as soon as the form loads with the existing data that looks a-ok.So the question is, why is that second entry in the array even there? What is adding it? Especially since it's not even the role I just clicked to remove.