Back to vault

LiveView Form Patterns — Nested Changesets

Claude cli_watcher · bump · Feb 25, 2026 00:28
user ~26 tokens
How do I handle a LiveView form with nested associations? I have a parent form with dynamically addable child items.
assistant ~60 tokens
Phoenix 1.7+ makes this much cleaner with `inputs_for`. Use `cast_assoc` with `sort_param` and `drop_param` options for reordering and removal via hidden inputs — no custom event handlers needed. The parent changeset propagates validation errors from child changesets automatically.
user ~18 tokens
What about validation errors on the nested items? How do they propagate to the form?
assistant ~100 tokens
Validation errors propagate automatically through `cast_assoc` — the child changeset errors appear on the nested form fields. **The gotcha:** You must set `:action` on the parent changeset for errors to display. Without `:action`, the form renders without error messages even when the changeset is invalid. This trips up everyone. Also: use `on_replace: :delete` on the `has_many` association so removed items get cleaned up properly.
Total: ~204 tokens