Skip to content

Fix(getDirtyFields): submit fields with null values when using useForm#13079

Merged
bluebill1049 merged 1 commit intoreact-hook-form:masterfrom
jonathanarnault:fix/12815-submit-null-values
Oct 6, 2025
Merged

Fix(getDirtyFields): submit fields with null values when using useForm#13079
bluebill1049 merged 1 commit intoreact-hook-form:masterfrom
jonathanarnault:fix/12815-submit-null-values

Conversation

@jonathanarnault
Copy link
Copy Markdown
Contributor

Problem

Fixes #12815

Fields with null values are not submitted when using useForm({ values }) since v7.55.0.

Before:

const { control, handleSubmit } = useForm({ values: { views: null } });

On submit: {} (null value is omitted)

Expected:
On submit: { views: null }

Root Cause:
In src/logic/getDirtyFields.ts, the markFieldsDirty function used !isNullOrUndefined(data[key]) which excluded null values from being marked as dirty, causing them to be omitted during form submission.

Solution

Changed the condition to only exclude undefined values:

} else if (!isUndefined(data[key])) {
  fields[key] = true;
}

This allows null to be treated as a valid field value while still excluding undefined fields.

Testing

Added 8 tests:

  • 2 unit tests in src/__tests__/logic/getDirtyFields.test.ts
  • 6 integration tests in src/__tests__/useForm/useFormWithNullValues.test.tsx

All 968 tests pass.

Changes

  • src/logic/getDirtyFields.ts: Changed condition in markFieldsDirty
  • src/__tests__/logic/getDirtyFields.test.ts: Added unit tests
  • src/__tests__/useForm/useFormWithNullValues.test.tsx: Added integration tests

Backward compatible, no breaking changes.

Copy link
Copy Markdown
Member

@bluebill1049 bluebill1049 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@bluebill1049 bluebill1049 merged commit c2a3509 into react-hook-form:master Oct 6, 2025
6 checks passed
@jonathanarnault jonathanarnault deleted the fix/12815-submit-null-values branch October 7, 2025 06:59
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.

issue: fields with value null are no longer submitted when using useForm({ values })

2 participants