Skip to content
Source
Sign inSign up
TanStack/form

Falsy values are being turned into undefined upon removing array field #1439

Send to my agent
Closedjuanvilladev(GitHub profile, opens in a new tab) opened this issue 2 comments

Describe the bug

When removing array elements, currently subfields with falsy values on the last row are being changed to undefined. Added a condition to specifically check for undefined.

This is critical as the casting converting booleans or empty strings to undefined leads to uncontrolled inputs in React and overall strange behavior regarding validators that expect particular value types.

Your minimal, reproducible example

https://stackblitz.com/edit/vitejs-vite-6huqeq8i?file=src%2FApp.tsx,src%2Fcomponents%2FForm.tsx

Steps to reproduce

  1. Click the push value button twice so you have three rows.
  2. Click the Log button and inspect the console.
  3. Notice that interests logs:
[
    {
        "id": "01965a86-4995-768a-85b9-be02512c2208",
        "interestName": ""
    },
    {
        "id": "01965a86-4ab7-751b-9368-8ce7539f2e49"
        "interestName": undefined
    }
]

interestName is now missing! It was overwritten as undefined

Expected behavior

Falsy values remain stable after removing field value.

How often does this bug happen?

None

Screenshots or Videos

No response

Platform

Replicatable on any code sandbox/React environment

TanStack Form adapter

react-form

TanStack Form version

1.6.3

TypeScript version

5.7.2

Additional context

No response

  1. Stumbled on the same error while using @tanstack/react-form-nextjs version 1.28.5.

    In my case, the onChange validation was triggered when removing item (another bug?), and the value got stuck in validation (it was expecting a boolean).

    It’s tricky because the UI representation of the item is removed, so there’s nowhere the error can be displayed. You can notice it because form submission is blocked due to the invalid field.

    <props.group.AppField
      name="my-value"
      validators={{
        onChange:
          Schema.entries.myValue,
      }}
    >
      {() => <CheckboxField label="My value" />}
    </props.group.AppField>

    Any updates on this issue? Does anyone have ideas for a temporary workaround?

  2. LeCarbonator(GitHub profile, opens in a new tab) added labelsv2: needs investigation

  3. Seems to be fixed as of v1.33.5, and is also fixed in v2. I'll close the issue for now since it seems to be stale.

    I assume the step you didn't specify is to remove the second entry before logging.

    Let me know if that's a mistake, I'll open the issue again.

Sign in to join this conversation.