Cannot use a live_component inside of a form_for

When I try to use a live_component inside of a form_for/4 block I see the following error:

A component must always be returned directly as part of a LiveView template.

The form_for/4 block ends with an anonymous function that breaks live_component
<%= form_for @changeset, “#”, [phx_change: :validate, phx_submit: :save, phx_target: @myself], fn f → %>

The form_for/3 function is deprecated but would probably work. Removal of the form_for/3 deprecation or a fix would be appreciated.

I found a solution. You should not use form_for/4 when working with liveview.
You should use the form component instead.

<.form let={f} for={@changeset} phx-change={:validate} phx-submit={:save} phx-target={@myself}></.form>

3 Likes