KeyError at GET /users/register key :name not found in: %{

I’ve set a new phoenix project and used gen_auth to create a liveview login/register

mix phx.gen.auth Accounts User users

when accessing the /users/register path i get the following error

KeyError at GET /users/register key :name not found in: %{

If i manually add the :name in the input form, other attributes like id or value will complain

I’ve checked this thread (KeyError) key :name not found in but putting an empty map will not work.

The code is out of the box, no changes were made so there must be a bug.

Can you please show code of a router and a full stacktrace of this error?

Hi, here is the info

routes

## Authentication routes

  scope "/", Teste119Web do
    pipe_through [:browser, :redirect_if_user_is_authenticated]

    live_session :redirect_if_user_is_authenticated,
      on_mount: [{Teste119Web.UserAuth, :redirect_if_user_is_authenticated}] do
      live "/users/register", UserRegistrationLive, :new
      live "/users/log_in", UserLoginLive, :new
      live "/users/reset_password", UserForgotPasswordLive, :new
      live "/users/reset_password/:token", UserResetPasswordLive, :edit
    end

    post "/users/log_in", UserSessionController, :create
  end

  scope "/", Teste119Web do
    pipe_through [:browser, :require_authenticated_user]

    live_session :require_authenticated_user,
      on_mount: [{Teste119Web.UserAuth, :ensure_authenticated}] do
      live "/users/settings", UserSettingsLive, :edit
      live "/users/settings/confirm_email/:token", UserSettingsLive, :confirm_email
    end
  end

  scope "/", Teste119Web do
    pipe_through [:browser]

    delete "/users/log_out", UserSessionController, :delete

    live_session :current_user,
      on_mount: [{Teste119Web.UserAuth, :mount_current_user}] do
      live "/users/confirm/:token", UserConfirmationLive, :edit
      live "/users/confirm", UserConfirmationInstructionsLive, :new
    end
  end

stacktrace

[info] GET /users/register
[debug] Processing with Teste119Web.UserRegistrationLive.new/2
  Parameters: %{}
  Pipelines: [:browser, :redirect_if_user_is_authenticated]
[info] Sent 500 in 155ms
[error] #PID<0.715.0> running Phoenix.Endpoint.SyncCodeReloadPlug (connection #PID<0.714.0>, stream id 1) terminated
Server: localhost:4000 (http)
Request: GET /users/register
** (exit) an exception was raised:
    ** (KeyError) key :name not found in: %{
  label: "Email",
  type: "email",
  prompt: nil,
  field: %Phoenix.HTML.FormField{
    id: "user_email",
    name: "user[email]",
    errors: [],
    field: :email,
    form: %Phoenix.HTML.Form{
      source: #Ecto.Changeset<
        action: nil,
        changes: %{},
        errors: [
          password: {"can't be blank", [validation: :required]},
          email: {"can't be blank", [validation: :required]}
        ],
        data: #Teste119.Accounts.User<>,
        valid?: false
      >,
      impl: Phoenix.HTML.FormData.Ecto.Changeset,
      id: "user",
      name: "user",
      data: #Teste119.Accounts.User<
        __meta__: #Ecto.Schema.Metadata<:built, "users">,
        id: nil,
        email: nil,
        confirmed_at: nil,
        inserted_at: nil,
        updated_at: nil,
        ...
      >,
      hidden: [],
      params: %{},
      errors: [],
      options: [method: "post"],
      index: nil,
      action: nil
    },
    value: nil
  },
  rest: %{required: true},
  __changed__: nil,
  inner_block: [],
  __given__: %{
    label: "Email",
    type: "email",
    field: %Phoenix.HTML.FormField{
      id: "user_email",
      name: "user[email]",
      errors: [],
      field: :email,
      form: %Phoenix.HTML.Form{
        source: #Ecto.Changeset<
          action: nil,
          changes: %{},
          errors: [
            password: {"can't be blank", [validation: :required]},
            email: {"can't be blank", [validation: :required]}
          ],
          data: #Teste119.Accounts.User<>,
          valid?: false
        >,
        impl: Phoenix.HTML.FormData.Ecto.Changeset,
        id: "user",
        name: "user",
        data: #Teste119.Accounts.User<
          __meta__: #Ecto.Schema.Metadata<:built, "users">,
          id: nil,
          email: nil,
          confirmed_at: nil,
          inserted_at: nil,
          updated_at: nil,
          ...
        >,
        hidden: [],
        params: %{},
        errors: [],
        options: [method: "post"],
        index: nil,
        action: nil
      },
      value: nil
    },
    __changed__: nil
  },
  multiple: false
}
        (teste119 0.1.0) lib/teste119_web/components/core_components.ex:350: anonymous fn/2 in Teste119Web.CoreComponents."input (overridable 1)"/1
        (teste119 0.1.0) /Users/macuserid/dev/elixir/teste119/lib/teste119_web/live/user_registration_live.ex:34: Teste119Web.UserRegistrationLive.render/1
        (elixir 1.15.2) lib/enum.ex:2510: Enum."-reduce/3-lists^foldl/2-0-"/3
        (phoenix_live_view 0.19.3) lib/phoenix_live_view/diff.ex:384: Phoenix.LiveView.Diff.traverse/7
        (phoenix_live_view 0.19.3) lib/phoenix_live_view/diff.ex:538: anonymous fn/4 in Phoenix.LiveView.Diff.traverse_dynamic/7
        (elixir 1.15.2) lib/enum.ex:2510: Enum."-reduce/3-lists^foldl/2-0-"/3
        (phoenix_live_view 0.19.3) lib/phoenix_live_view/diff.ex:384: Phoenix.LiveView.Diff.traverse/7
        (phoenix_live_view 0.19.3) lib/phoenix_live_view/diff.ex:538: anonymous fn/4 in Phoenix.LiveView.Diff.traverse_dynamic/7
        (elixir 1.15.2) lib/enum.ex:2510: Enum."-reduce/3-lists^foldl/2-0-"/3
        (phoenix_live_view 0.19.3) lib/phoenix_live_view/diff.ex:384: Phoenix.LiveView.Diff.traverse/7
        (phoenix_live_view 0.19.3) lib/phoenix_live_view/diff.ex:538: anonymous fn/4 in Phoenix.LiveView.Diff.traverse_dynamic/7
        (elixir 1.15.2) lib/enum.ex:2510: Enum."-reduce/3-lists^foldl/2-0-"/3
        (phoenix_live_view 0.19.3) lib/phoenix_live_view/diff.ex:384: Phoenix.LiveView.Diff.traverse/7
        (phoenix_live_view 0.19.3) lib/phoenix_live_view/diff.ex:538: anonymous fn/4 in Phoenix.LiveView.Diff.traverse_dynamic/7
        (elixir 1.15.2) lib/enum.ex:2510: Enum."-reduce/3-lists^foldl/2-0-"/3
        (phoenix_live_view 0.19.3) lib/phoenix_live_view/diff.ex:384: Phoenix.LiveView.Diff.traverse/7
        (phoenix_live_view 0.19.3) lib/phoenix_live_view/diff.ex:538: anonymous fn/4 in Phoenix.LiveView.Diff.traverse_dynamic/7
        (elixir 1.15.2) lib/enum.ex:2510: Enum."-reduce/3-lists^foldl/2-0-"/3

That’s odd, that missing :name key should be set by the the first input/2 function head in the default core_components.ex.

  def input(%{field: %Phoenix.HTML.FormField{} = field} = assigns) do
    assigns
    |> assign(field: nil, id: assigns.id || field.id)
    |> assign(:errors, Enum.map(field.errors, &translate_error(&1)))
    |> assign_new(:name, fn -> if assigns.multiple, do: field.name <> "[]", else: field.name end) // `:name` key should be set here
    |> assign_new(:value, fn -> field.value end)
    |> input()
  end

What do you have in your core_components.ex for input/2?

My input function looks different from yours
All the code was generated with mix phx.new teste119
How to get your version??

  def input(%{field: {f, field}} = assigns) do
    assigns
    |> assign(field: nil)
    |> assign_new(:name, fn ->
      name = Phoenix.HTML.Form.input_name(f, field)
      if assigns.multiple, do: name <> "[]", else: name
    end)
    |> assign_new(:id, fn -> Phoenix.HTML.Form.input_id(f, field) end)
    |> assign_new(:value, fn -> Phoenix.HTML.Form.input_value(f, field) end)
    |> assign_new(:errors, fn -> translate_errors(f.errors || [], field) end)
    |> input()
  end

the input looks like this

<.input field={@form[:email]} type="email" label="Email" required />

Make sure your installer is up to date. You can check the installed version with mix archive and install a different version with mix archive.install hex phx_new <version>.

Thanks for pointing to the right direction, my mix archive was * hex-2.0.6

I’ve run the following with latest phoenix

mix archive.install hex phx_new 1.7.6

then

mix phx.new test176

and also

mix phx.gen.auth Accounts User users

And its now working.

Bonus tip, mix local.phx is equivalent to mix archive.install hex phx_new and is bit easier to remember.

thanks for the tip :+1: