Skip to content

fix(core): keep form submit callbacks in foreach scope - #7687

Draft
DevChiniwala wants to merge 1 commit into
reflex-dev:mainfrom
DevChiniwala:fix/foreach-form-submit
Draft

DevChiniwala wants to merge 1 commit into
reflex-dev:mainfrom
DevChiniwala:fix/foreach-form-submit

Conversation

@DevChiniwala

@DevChiniwala DevChiniwala commented Oct 28, 2026 •

Copy link
Copy Markdown

All Submissions

  • Followed CONTRIBUTING.md and the agent guidelines.
  • Checked for another PR fixing this issue.

Type of change

  • Bug fix

Description

Fixes #3210. Submitting a form inside rx.foreach can raise ReferenceError: item_rx_state_ is not defined instead of sending the row's values to the backend.

The submit callback was emitted as a hook outside the loop, where the row item and index do not exist. Render it inline on the form so it closes over the row that created it. The old useCallback had no dependency array, so it already recreated its callback on each render.

Form-data collection, refs and reset behavior use the existing implementation. The legacy submit-name property remains accepted. Tests that inspected the former hook now inspect the rendered handler and retain their assertions. The component stub hash and package news fragment are updated.

Changes to core features

  • Added an explanation and regression tests.
  • Ran the affected tests locally.
  • Reviewed the patch against the contribution guidelines.

Validation

  • New unit regression: both form types fail before the fix.
  • Browser control on unchanged source: submission raises the out-of-scope item error.
  • Four headless browser cases pass: HTML/Radix forms in development/production, with state-backed rows submitted out of order. Item, index, fields and reset behavior are checked.
  • Four existing form-browser cases pass.
  • All 206 affected tests pass on Windows and Linux. All nine Linux pre-commit hooks pass, including Pyright, ty and code generation.

Remaining baseline gates

Kept as a draft. The full Linux unit run has 15,172 passes, 78 skips and one failure in the unrelated Granian backend-restart test. That same test fails on unchanged source too: recovery fails on main and in the full patched run; a separate patched control fails at the refusal step. The backend implementation and that test are untouched. Related readiness work already has an active PR, #7416.

The broad Windows run also encountered a local long-path file-creation limit, independently reproduced without importing Reflex. Two former-hook assertions from that run were updated and pass in the final affected suites. Upstream CI remains pending.

View guided diff

Related-work reconciliation — October 30

Related architecture work: #7690 addresses #3210 with per-item compiler scopes and is stacked on #6936. This draft keeps form-submit callbacks inline in their row scope. The maintainer's preferred compiler architecture should determine whether this focused fix is useful before or alongside that broader work.

A form submit handler generated as a hoisted useCallback cannot see the
item or index bound by a nested foreach map. Render the same handler body
inline on the form, retaining its event/ref/reset VarData, so the callback
closes over its row. The former hook had no dependency array and recreated
its function on each render already. Keep the legacy submit-name property
accepted rather than removing a component prop in this bug fix.

Cover both item/index captures with a regression that fails on the old
handler. Extend the existing form app for state-backed row submissions in
development/production with both HTML and Radix forms; check out-of-order
submissions, fields and reset behavior. The unchanged component produces
an out-of-scope item ReferenceError. All four new browser cases and four
existing form-browser cases pass with the fix. The existing literal-loop
generated-JavaScript control also retains all three row indices.

Move existing hook-location assertions to the rendered handlers, retaining
control-ref and extra-event checks. Check the order of appended events.
All 206 affected tests pass on Windows and Linux. All nine full Linux
pre-commit hooks pass, including codegen, Pyright and ty.

The broader Linux run has 15,172 passing tests, 78 skips and one unrelated
backend-restart failure. Unchanged source fails at its recovery assertion
too; a separate patched isolation run fails at the refusal assertion.
The backend source and test match main byte-for-byte. Do not weaken or
skip them. Related backend-readiness work already has active PR reflex-dev#7416.

The initial broader Windows run had 14,147 passes, 83 skips, two tests
inspecting the old hooks (now updated and passing in the affected suite),
and a long-path file-creation failure. A standalone file-I/O control
reproduces the local OS limit without importing Reflex. Native Windows
aggregate typechecking also reports unrelated platform/private-import
diagnostics; the full Linux presubmit passes.

Fixes reflex-dev#3210.
@greptile-apps

greptile-apps Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no actionable issues were found.

Summary

Form submit handlers now render inline, keeping each row’s item and index available inside rx.foreach.

  • Forms in rx.foreach now submit their row values with the form data.

Reviews (1) · Last reviewed commit: "fix(core): keep form submit callbacks in..." · Reviewed by Greptile

@DevChiniwala

Copy link
Copy Markdown
Author

@masenf — this addresses #3210, which you confirmed still affected 0.9.2. Both HTML and Radix forms now retain the row item and index; browser tests cover state-backed rows in development and production, submitted out of order with their form fields and reset behavior.

All 206 affected tests pass on Windows and Linux, the four new and four existing browser cases pass, and all Linux pre-commit hooks pass. Greptile's review found no actionable issues.

I am keeping this draft because the broader unit run encounters the existing Granian restart-test failure described in the PR. The unchanged component reproduces the foreach error; unchanged backend code reproduces the separate backend failure. The backend source/test are untouched, and related work already has #7416. Fork CI still needs approval. Guidance on that baseline gate would help complete validation.

Prepared with Codex assistance.

This branch has not been deployed

No deployments
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.

[REF-2745] rx.form handle_submit lambda cannot reference foreach vars

1 participant