Fix infinite loop in form widget with circular dependsOn declarations#1478
Fix infinite loop in form widget with circular dependsOn declarations#1478austinderrick wants to merge 2 commits intowintercms:developfrom
Conversation
When two or more form fields have circular dependsOn declarations (e.g., field A depends on B and B depends on A), the onRefreshDependants method triggers change events that cascade infinitely, freezing the browser tab. This fix adds cascade chain tracking through the jQuery event object. Each time a field triggers its dependants, the current field name is appended to a cascadeChain array on the event. Before refreshing, the method checks whether the triggering field already appears in the chain. If it does, a cycle has been detected and the cascade stops. This preserves legitimate transitive cascading (A -> B -> C) while preventing circular loops (A -> B -> A) of any depth. The event parameter was already being passed by jQuery as the third argument to the handler (after the two preset arguments from $.proxy), so no changes to the event binding are needed. Fixes wintercms#421
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
✅ Files skipped from review due to trivial changes (1)
WalkthroughonRefreshDependants in modules/backend/widgets/form/assets/js/winter.form.js was changed to accept a third Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@modules/system/tests/js/fixtures/formWidget/FormWidgetStubs.js`:
- Around line 18-26: The jQuery.fn.request stub in FormWidgetStubs.js mismatches
the real contract (production $.fn.request returns a jqXHR with
.done/.fail/.always and is async) and currently uses a synchronous .success()
call; update the stub so it returns a jQuery.Deferred-based object exposing
.done(), .fail(), and .always() (or make .success() an alias to .done()) and
ensure callbacks are invoked asynchronously (e.g., resolve/reject via setTimeout
or Deferred.resolve()/reject() so handlers run async) so tests mirror real
$.ajax/jqXHR behavior and surface race/ordering bugs; target the
jQuery.fn.request implementation in this file when making the change.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: fe7242e1-cb2d-4a35-a506-b8dfcec2b8a2
📒 Files selected for processing (3)
modules/backend/widgets/form/assets/js/winter.form.jsmodules/system/tests/js/cases/framework/FormWidgetDependants.test.jsmodules/system/tests/js/fixtures/formWidget/FormWidgetStubs.js
Updated the $.fn.request test stub to use jQuery.Deferred properly: - .success() is now an alias for .done() (matching the WinterCMS framework.js Request class contract) - Deferred is resolved immediately via deferred.resolve() - Exposes .done(), .fail(), .always() from the jQuery Deferred API
Summary
When two or more form fields have circular
dependsOndeclarations (e.g., field A depends on B and B depends on A), theonRefreshDependantsmethod triggers change events that cascade infinitely, freezing the browser tab.This fix adds cascade chain tracking through the jQuery event object. Each time a field triggers its dependants, the current field name is appended to a
cascadeChainarray on the event. Before refreshing, the method checks whether the triggering field already appears in the chain. If it does, a cycle has been detected and the cascade stops.This preserves legitimate transitive cascading (A → B → C) while preventing circular loops (A → B → A) of any depth.
How it works
The
eventparameter was already being passed by jQuery as the third argument to the handler (after the two preset arguments from$.proxy), so no changes to the event binding inbindDependantswere needed. The only changes are inonRefreshDependants:eventparameter and extractcascadeChain(defaults to[]for user-initiated changes)fieldNameis already incascadeChain, return early (cycle detected)fieldNameto the chain and pass it through the triggered change eventsTests
Added 4 Jest tests covering:
All 44 tests pass (7 suites including the new one).
Fixes #421
Summary by CodeRabbit
Bug Fixes
Tests