Skip to content

Fix NestedForm falling back to the parent model when empty (#1522) - #1523

Merged
LukeTowers merged 1 commit into
developfrom
fix/nestedform-empty-data-model-fallback
Aug 21, 2026
Merged

Fix NestedForm falling back to the parent model when empty (#1522)#1523
LukeTowers merged 1 commit into
developfrom
fix/nestedform-empty-data-model-fallback

Conversation

@LukeTowers

@LukeTowers LukeTowers commented Aug 21, 2026

Copy link
Copy Markdown
Member

Problem

Fixes #1522.

A nestedform field whose sub-field name collides with a model attribute crashes the whole form with ErrorException: Array to string conversion when the nested form has no value — most visibly when the model attribute is a $jsonable array.

Root cause

NestedForm::init() passed the field's load value straight to the inner Form's data:

$config->data = $this->getLoadValue();

When the field has no value, getLoadValue() returns null. Form::getModel() then does:

$this->data = isset($this->data) ? (object) $this->data : $this->model;

isset() is false for null, so the inner form falls back to the parent model as its data source. Sub-fields then resolve their values from the model's attributes by name, so a sub-field named e.g. content picks up $model->content — and rendering an array value as a textarea throws Array to string conversion, taking down the form.

Fix

Scope the nested form to its own value; pass an empty array instead of null so it never reaches the parent model:

$config->data = $this->getLoadValue() ?: [];

Tests

Adds modules/backend/tests/formwidgets/NestedFormScopingTest.php:

  • testEmptyNestedFormDoesNotResolveSubFieldsFromParentModel — an empty nested form with a content sub-field, on a model whose root content is a jsonable array, must resolve the sub-field to empty (not the model's array). Fails without the fix ("two arrays are not identical"), passes with it.
  • testPopulatedNestedFormStillResolvesItsOwnValues — a populated nested form still resolves its own values (the nested value wins over the colliding model attribute).

Full modules/backend/tests/widgets + modules/backend/tests/formwidgets suites pass (85 tests).

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Fixed empty nested forms incorrectly resolving fields from the parent record.
    • Prevented crashes when nested field names overlap with parent model attributes.
    • Preserved correct value handling for populated nested forms.
  • Tests

    • Added coverage for empty and populated nested form field scoping.

An empty nestedform passed its inner Form a null data source, so Form::getModel()
fell back to the parent model. Sub-fields then resolved their values from the
model's attributes by name — and when a sub-field name collided with a model
attribute holding an array (e.g. a jsonable field), rendering it (as a textarea,
etc.) threw "Array to string conversion", taking down the whole form.

Pass an empty array rather than null when the field has no value, so the inner
form stays scoped to the nested data and never reaches the parent model.

Fixes #1522

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Aug 21, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: b4ac0741-0597-4624-be34-b23549ed71a2

📥 Commits

Reviewing files that changed from the base of the PR and between c0f5126 and dd3c633.

📒 Files selected for processing (2)
  • modules/backend/formwidgets/NestedForm.php
  • modules/backend/tests/formwidgets/NestedFormScopingTest.php

Included review availability: Your plan provides up to 4 included reviews per hour; 3 remain after this review.


Walkthrough

NestedForm::init() now passes an empty array to the inner Form when the nested field has no value. This prevents the inner form from using the parent model as its data source. Added tests cover empty nested forms with colliding parent attributes and populated nested forms that retain their own values.

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: ⚪ Minimal · up to dd3c6

The change narrowly prevents empty nested forms from reading values from the parent model while preserving populated nested-form behavior; no actionable merge-blocking risk remains after normal checks and review.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 16.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 6 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the NestedForm fallback fix and matches the primary change.
Linked Issues check ✅ Passed The change prevents parent-model fallback for empty NestedForm data and preserves populated values, satisfying issue #1522.
Out of Scope Changes check ✅ Passed The production change and regression tests are directly related to the NestedForm scoping issue in #1522.
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/nestedform-empty-data-model-fallback

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@LukeTowers
LukeTowers merged commit 4f6e440 into develop Aug 21, 2026
17 checks passed
@LukeTowers
LukeTowers deleted the fix/nestedform-empty-data-model-fallback branch August 21, 2026 19:13
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.

NestedForm sub-field crashes with "Array to string conversion" when its name collides with a model jsonable attribute

1 participant