Skip to content

Conversation

@Jszigeti
Copy link
Contributor

Problem

When using <Form> or <SimpleForm> with resetOptions={{ keepDirtyValues: true }} (typically for AutoSave), dirty form values were lost when the record was refetched.

Scenario:

  1. User opens a form and starts typing in a field (form becomes dirty)
  2. An action triggers a record refetch (e.g., changing a related field, external update)
  3. The user's unsaved input disappears — overwritten by server data

This is a follow-up fix to #8911. While that PR forwarded resetOptions to useForm, it didn't pass it to the manual reset() call in the useEffect, causing keepDirtyValues to be ignored when the record is refetched.


Solution

  • Fixed useAugmentedForm.ts to pass resetOptions to reset() when the record changes
  • Added logic to detect record ID changes vs refetches:
    • Same record ID (refetch): apply resetOptions (e.g., keepDirtyValues: true)
    • Different record ID (navigation): do a full reset to avoid carrying dirty values from the previous record
  • Added resetOptions to the UseFormOwnProps interface

Added unit tests in Form.spec.tsx covering:

  • keepDirtyValues: true preserves user input during refetch (same ID)
  • Without keepDirtyValues, server values overwrite user input
  • Switching to a different record (different ID) always does a full reset

How To Test

  1. Create a form with resetOptions={{ keepDirtyValues: true }} and AutoSave enabled
  2. Modify a field (make the form dirty)
  3. Trigger a record refetch (e.g., by changing a related field that causes invalidation)
  4. Expected result: The modified field value is preserved, not overwritten by server data
  5. Run tests:
    • make test - Unit tests pass (2595 passed, 9 unrelated failures on master)
    • npx jest packages/ra-core/src/form/Form.spec.tsx - All 40 tests pass

Additional Checks

  • The PR targets master for a bugfix
  • The PR includes unit tests (Form.spec.tsx)
  • No story update required (visual behavior unchanged)
  • No documentation change required (fixes existing documented behavior that wasn't working)

Notes

  • Root cause: Simplify form reset on record change, and forward supported props from useAugmentedForm to useForm #8911 forwarded resetOptions to useForm, but the useEffect that manually calls reset() when the record changes didn't pass resetOptions to the reset() call.
  • The fix also ensures that when navigating to a different record (ID change), a full reset is performed regardless of resetOptions. This prevents dirty values from record A being incorrectly preserved when viewing record B.
  • No breaking changes.
  • Fixes data loss issue affecting forms with AutoSave + external record updates.

I'm happy to adjust anything if needed 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant