phoenixframework / phoenix

Peace of mind from prototype to production
https://www.phoenixframework.org
MIT License
21.45k stars 2.88k forks source link

Simple form error when try to use the %Plug.Con in the 'for' #5906

Closed pmareke closed 3 months ago

pmareke commented 3 months ago

Environment

Actual behavior

If in an new Phoenix project (I'm following the example from the Phoenix Programming 1.4 but with the latest Phoenix version) I try to create a simple_form with the %Plug.Conn{} struct in the for property:

<.simple_form
  :let={f}
  for={@conn}
  as={:session}
  phx-change="validate"
  action={~p"/sessions"}>
  <.input field={f[:username]} label="Username" />
  <.input field={f[:password]} type="password" label="Password" />

  <:actions>
    <.button>Log In</.button>
  </:actions>
</.simple_form>

The app crashes with the following error

Protocol.UndefinedError at GET /sessions/new
protocol Phoenix.HTML.FormData not implemented for %Plug.Conn{adapter: {Bandit.Adapter, :...}, assigns: %{connection: %{}, layout: {RumblWeb.Layouts, "app"}, ...

Expected behavior

As soon as I used en empty struct or a nil, the app works perfectly.

<.simple_form
  :let={f}
  for={#{}}
  as={:session}
  phx-change="validate"
  action={~p"/sessions"}>
  <.input field={f[:username]} label="Username" />
  <.input field={f[:password]} type="password" label="Password" />

  <:actions>
    <.button>Log In</.button>
  </:actions>
</.simple_form>

PD: I replicate the error in a new Phoenix ap, without anything else rather than the simple_form and the error happens in the same way.

SteffenDE commented 3 months ago

Hi @pmareke,

support for passing a conn to forms was removed in phoenix_html 4.0: https://github.com/phoenixframework/phoenix_html/commit/0be2c6f1fab4fa3655de4bae90ae0a87649c8779#diff-70098bfc9e8cc88bd84dc5bc85760a5599409295be3232468810d6a81fa515c2L85

This should probably be part of the phoenix_html changelog...

Instead of @conn, you can simply pass @conn.params instead.