Skip to content

Comments

fix(reset): preserve isValid state when keepIsValid option is used#13173

Merged
bluebill1049 merged 2 commits intoreact-hook-form:masterfrom
a28689604:fix/reset-keep-is-valid
Dec 8, 2025
Merged

fix(reset): preserve isValid state when keepIsValid option is used#13173
bluebill1049 merged 2 commits intoreact-hook-form:masterfrom
a28689604:fix/reset-keep-is-valid

Conversation

@a28689604
Copy link
Contributor

@a28689604 a28689604 commented Dec 2, 2025

Proposed Changes

I noticed that the keepIsValid reset option wasn’t working correctly. After calling reset, the isValid state should remain preserved when keepIsValid is enabled. Since I couldn’t find any existing examples or test coverage for this behavior, I added two test cases to verify it. Please review and confirm whether these tests reflect the expected behavior.

Changes

I added a keepIsValid flag to createFormControl, which is set only when keepStateOptions.keepIsValid is true. When this flag is active, the _setValid function (typically triggered after the re-render following a reset) will exit early, preventing the valid state from being updated.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing tests pass locally with my changes

Comment on lines +185 to +187
if (_state.keepIsValid) {
return;
}
Copy link
Member

Choose a reason for hiding this comment

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

Isn't this going to keep form validity form state being blocked?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, but the blocking only happens when reset is called with { keepIsValid: true }. If a later reset does not use that option, it won’t be blocked and setValid will run normally. I’ve updated the test case to reflect this.

I’m still not entirely sure what the intended behavior of keepIsValid: true should be, since there are no existing test cases or documented examples. Please let me know if my understanding is off.

Copy link
Member

Choose a reason for hiding this comment

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

sgtm! i will check the pr again.

a28689604 and others added 2 commits December 7, 2025 15:43
- Add keepIsValid flag to internal _state to track when isValid should be preserved
- Skip validation recalculation in _setValid when keepIsValid is active
- Set flag during reset when keepStateOptions.keepIsValid is true
- Add test cases for per-call and form-level keepIsValid options
- Update reset test cases to include separate buttons for resetting with and without the keepIsValid option.
- Verify form state and validation status after each reset action to ensure correct behavior.
- Ensure that the isValid state is preserved or cleared as expected based on the reset options used.

Signed-off-by: a28689604 <[email protected]>
@a28689604 a28689604 force-pushed the fix/reset-keep-is-valid branch from d1a791c to e4fd245 Compare December 7, 2025 07:43
@bluebill1049 bluebill1049 merged commit 64eec9b into react-hook-form:master Dec 8, 2025
6 checks passed
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.

2 participants