Fix Infinite Loop When Accessing Parent in Nested Form Validations#109
Open
adz624 wants to merge 4 commits intotrailblazer:masterfrom
Open
Fix Infinite Loop When Accessing Parent in Nested Form Validations#109adz624 wants to merge 4 commits intotrailblazer:masterfrom
adz624 wants to merge 4 commits intotrailblazer:masterfrom
Conversation
Filter out 'parent' field when collecting validation errors from nested fields to prevent infinite recursion. Also improve test assertion style. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Clean up commented old implementation line. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes a critical infinite loop issue that occurs when using the
Disposable::Twin::Parentfeature with nested form validations. The issue causesa stack overflow error when attempting to collect validation error messages from forms that have parent-child relationships.
Problem
When using
Disposable::Twin::Parentfeature in nested forms (e.g., a collection of child forms within a parent form), attempting to retrieve validationerror messages via
form.errors.full_messagesresults in an infinite loop and eventual stack overflow.Example Scenario
When calling
form.validate(...)followed by accessingform.errors.full_messages, the application would hang due to infinite recursion.Root Cause
The issue is in the
full_messages_for_nested_fieldsmethod inlib/reform/form/active_model/validations.rb:Before the fix:
The method iterates through all form fields to collect error messages from nested forms. However, when
Disposable::Twin::Parentis enabled:songs)parentreference back to the parent formThe
parentfield is not a regular data field—it's a structural reference added byDisposable::Twin::Parentto enable child forms to access parent dataduring validation. Including it in error message collection causes the infinite loop.
Solution
Filter out the
parentfield when collecting nested field errors:After the fix:
Why This Works
parentfield is not a data field that can have validation errorsparentfield breaks the circular reference chainChanges
Modified Files
lib/reform/form/active_model/validations.rb (lines 180-184)
.to_ato ensureform_fieldsis converted to an array.reject { |field| field[0] == "parent" }to filter out parent referencestest/parent_test.rb (new file)