Skip to content

canSubmit is not set in state when provided to defaultState prop at form initialization #944

Description

@JortRoelofs

Describe the bug

I'm trying to create a read only version of a form in React. react-hook-form has the disabled prop in the UseForm prop which propagates to all fields and I hope a similar feature could be added to Tanstack form. I wanted to emulate a read only form by setting canSubmit to false in the defaultState option. Then, by checking if both canSubmit and isSubmitting are false, the form must be read only. I hope my understanding about how those two fields work is correct.

However, I found out that the value for canSubmit is ignored when provided to defaultState. A simple example is as follows:

const form = useForm({
  defaultValues: {
    firstName: '',
    lastName: '',
  },
  defaultState: {
    canSubmit: false,
  },
});
console.log(form.state.canSubmit); // Evaluates to true

Your minimal, reproducible example

https://stackblitz.com/edit/tanstack-form-vzvauf?file=src%2Findex.tsx

Steps to reproduce

  1. Create a form object using the useForm hook with defaultState: { canSubmit: false } in the props.
  2. Evaluate the resulting value of form.state.canSubmit

Expected behavior

By submitting canSubmit: false in the defaultState, I expect the state after initialization to contain a false value for this property. The value is true however, which is not expected. If this is intentional, it is very counter-intuitive, but if this is unintentional then I think it needs fixing.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

  • OS: Windows
  • Browser: Chrome
  • Version: 128

TanStack Form adapter

react-form

TanStack Form version

0.32.0

TypeScript version

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions