Skip to content

🐞 fix: pass options parameter through setValues to enable validation#13457

Merged
bluebill1049 merged 2 commits into
react-hook-form:masterfrom
philibea:fix/setvalues-validation-options
May 19, 2026
Merged

🐞 fix: pass options parameter through setValues to enable validation#13457
bluebill1049 merged 2 commits into
react-hook-form:masterfrom
philibea:fix/setvalues-validation-options

Conversation

@philibea
Copy link
Copy Markdown
Contributor

@philibea philibea commented May 19, 2026

Description

This PR fixes the issue where setValues was not respecting the options parameter, specifically shouldValidate, shouldDirty, and shouldTouch.

Problem

When calling:

setValues({ field: value }, { shouldValidate: true })

The options parameter was being ignored, causing:

  • Validation not to be triggered
  • isValid form state not to update
  • Errors not to be cleared when values become valid
  • dirtyFields and touchedFields not to update

Solution

Modified the setValues function to:

  1. Accept the options parameter (already defined in the type)
  2. Pass options through to the internal _setValue function for each field
  3. Call _setValid() when shouldValidate: true to recalculate form validity

Tests (src/__tests__/useForm/setValues.test.tsx)

Added 3 comprehensive tests:

  1. Error clearing test - Validates email pattern, required, clears errors when valid
  2. Multiple fields test - Validates username, email, age fields simultaneously
  3. Opt-in validation test - Verifies validation only runs when shouldValidate: true

Related Issues

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
  • New and existing tests pass locally

philibea added 2 commits May 19, 2026 23:43
- Accept options parameter in setValues function
- Pass options to _setValue for each field
- Call _setValid() when shouldValidate is true
- Add tests for shouldValidate, shouldDirty, and shouldTouch options

This fixes the issue where setValues with shouldValidate: true
was not updating the isValid form state.
- Test error clearing when revalidating with valid values
- Test multiple fields validation with setValues
- Test that validation is not triggered without shouldValidate option
- Test pattern validation (email), minLength, min, and required rules

These tests verify that setValues with shouldValidate: true properly
clears errors and updates isValid state when fields become valid.

Signed-off-by: Alexandre Philibeaux <aphilibeaux@scaleway.com>
@bluebill1049
Copy link
Copy Markdown
Member

I didn't think user would need that options, but then i check the doc... i did put all the options there https://react-hook-form.com/docs/useform/setvalues could you include shouldDirty as well?

@bluebill1049
Copy link
Copy Markdown
Member

nvm you did. ty

@bluebill1049 bluebill1049 merged commit 778881c into react-hook-form:master May 19, 2026
6 checks passed
@philibea
Copy link
Copy Markdown
Contributor Author

philibea commented May 19, 2026

@bluebill1049 actually i still have a bug on this test on my side, and my Submit button is based on isValid from the form which is not revalidate after the setValues.

From your point of view, isValid should be recompute anyway ?

@bluebill1049
Copy link
Copy Markdown
Member

oh i think missing the subscription update there.

@bluebill1049
Copy link
Copy Markdown
Member

let me check it out.

@bluebill1049
Copy link
Copy Markdown
Member

hmm should just worked, i wrote a test and it pass.

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: setValues bug. setValues seems that the controller has not been notified to update the fields.

2 participants