Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[react-form] there is no way to clear a form value if it has a default value. #1289

Open
a-eid opened this issue Mar 16, 2025 · 7 comments
Open

Comments

@a-eid
Copy link

a-eid commented Mar 16, 2025

Describe the bug

useForm({
  defaultValues: {
    status: params.get('status') // assume this is active for example 
  }
})
      <form.Field name="status" asyncAlways>
        {(field) => (
        .... 
        field.setValue(undefined) // doesn't work. it resets status to active.
        form.setFieldValue("status", undefined) // doesn't work. it resets status to active.
        field.setValue(undefined) // doesn't work. it resets status to active.
        )}
      </form.Field> 

they all default to the default value

Your minimal, reproducible example

TODO

Steps to reproduce

N/A

Expected behavior

I would expect that setValue / handleChange would accept undefiend and it wouldn't be overridden by the default value.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

  • OS: Macos
  • version: ^1.0.5

TanStack Form adapter

None

TanStack Form version

^1.0.5

TypeScript version

No response

Additional context

No response

@vitassuper
Copy link

Consider to use null instead of undefined.

@LastGunslinger
Copy link

LastGunslinger commented Mar 19, 2025

@vitassuper I'm currently using v1.1.0 and also having this problem. In this case I do think that is is a bug, or if not, then it should probably be expressly spelled out in the docs that this is not supported. For example, I if I have a type like this:

type Exmaple = {
    first: string | undefined
    last: string | undefined
}

Where undefined is a valid value for one of the fields, and I set up a form with useForm:

const form = useForm({
    defaultValues: { first: 'John', last: 'Smith' } as Example,
   // useForm options
})

then I would think that undefined is a valid value for either field. But in the form itself, if I do something like this:

<form.Field name='first'>
    <CustomInput
        // Line below has unexpected behavior if value is undefined, and does not raise a type error because undefined IS a valid value
        onChange(value => field.handleChange(value))
    />
</form.Field>
field.handleChange(undefined)

Instead of setting the value of that field to undefined, it will instead be reverted to 'John'. This seems like a bug to me, especially since it is typed correctly, and other form libraries that I've used in the past have not had issues with this. While agree that using null instead of undefined is probably a better choice, this conflicts with a lot of form validation that my company uses with Zod's partial() property, which is usually where the undefined values are coming from. Unfortunately Zod does not have a good way currently of making all of an object's fields null-ish (null or undefined) , so for now the partial function is the best that we can do.

@a-eid
Copy link
Author

a-eid commented Mar 20, 2025

Consider to use null instead of undefined.

unfortunately neither null nor undefined work.

@LastGunslinger
Copy link

In my case, using null does work, but it required a refactor of my validation schemas to get there.

@a-eid
Copy link
Author

a-eid commented Mar 20, 2025

@LastGunslinger what changes did you make, my form element is optional and should acception undefined values. ?

@LastGunslinger
Copy link

LastGunslinger commented Mar 23, 2025

@a-eid I ended up changing the type of the defaultValues property in the useForm hook, from something like this:

type User {
    firstName?: string | undefined
    lastName?: string | undefined
}

to something like this:

type User {
    firstName?: string | null | undefined
    lastName?: string | null | undefined
}

Then I just made sure that all of my field.handleChange calls used null instead of undefined when called:

<CustomInput
    // {...otherProps}
    onChange={(value: string | null | undefined) => field.handleChange(value ?? null)}
/>

After making these changes I don't have the issue anymore, but it did require a substantial change for my project. My actual defaultValues type is not so simple, it's defined as Zod schema based on a larger type, but the idea for fixing it is the same.

@jashworth
Copy link

This seems like a bug to me, especially since it is typed correctly, and other form libraries that I've used in the past have not had issues with this.

field.handleChanged(undefined);
field.setValue(undefined);
form.setFieldValue("AField", undefined);
form.setFieldValue("AFieldContainingAnOptionalProp", { }); // prop is set to default value, NOT omitted

The fact that all these can result in the field being set to something other than undefined seems counterintuitive and likely to lead to some ugly workarounds. (I outline one here.)

Is the rationale behind this behaviour explained anywhere?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants