Skip to content

Conversation

@Dhavanesh24cs412
Copy link

  • Remove stale canSubmit check that prevented onSubmitInvalid from being called
  • On first render, canSubmit was true even with invalid default values

Problem

When a form is initialized with invalid default values, the onSubmitInvalid callback was not being called on the first submission attempt. This occurred because an early canSubmit check in the _handleSubmit method would short-circuit the validation flow before reaching the proper validation logic.

Root Cause

The issue was located in packages/form-core/src/FormApi.ts in the _handleSubmit method. An early check:

if (!this.state.canSubmit && !this._devtoolsSubmissionOverride) {
  // Early return without proper validation
  return
}

This check was preventing the form from:

  1. Calling validateAllFields('submit') to validate all fields
  2. Executing the onSubmitInvalid callback when validation fails
  3. Properly handling forms with invalid default values

Solution

Removed the stale early canSubmit check from the _handleSubmit method. The form now:

  • Proceeds directly to validateAllFields('submit')
  • Properly calls onSubmitInvalid when validation fails
  • Correctly handles submission attempts on forms with invalid default values
  • Still respects validation logic through proper checks after validation occurs

Changes Made

  • ✅ Removed early canSubmit guard in _handleSubmit method
  • ✅ Validation now properly executes on all submissions
  • onSubmitInvalid callback is invoked when appropriate
  • ✅ Fixes edge case with invalid default form values

Testing

This fix addresses the issue described in #1990 where forms with invalid default values were not triggering the onSubmitInvalid callback.

Closes #1990- Fixes: onSubmitInvalid not called when form is invalid with default values

🎯 Changes

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally with pnpm test:pr.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

…ck#1990)

- Remove stale canSubmit check that prevented onSubmitInvalid from being called
- On first render, canSubmit was true even with invalid default values
- Proper validation checks after this point correctly handle onSubmitInvalid
- Fixes: onSubmitInvalid not called when form is invalid with default values
@changeset-bot
Copy link

changeset-bot bot commented Jan 16, 2026

⚠️ No Changeset found

Latest commit: 8e4b380

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@Dhavanesh24cs412
Copy link
Author

Dhavanesh24cs412 commented Jan 16, 2026

This solution effectively addresses the issue where onSubmitInvalid wasn't being triggered for forms with invalid default values. By removing the early canSubmit check and allowing validation to execute properly, forms can now correctly invoke the callback even on initial submission. The approach maintains backward compatibility while fixing the edge case.

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

Successfully merging this pull request may close these issues.

onSubmitInvalid not called when canSubmit is false (v1.27.7)

1 participant