esacteksab

esacteksab

(KeyError) key :name not found in

I’ve been playing with Phoenix for a while now and I’ve encountered a problem that I simply don’t understand, so I decided to ask for help.

The entirety of the code base can be found here

The core of the issue is here, the error:

Request: GET /users/two_factor
** (exit) an exception was raised:
    ** (KeyError) key :name not found in: %{__changed__: nil, __given__: %{__changed__: nil, field: {%Phoenix.HTML.Form{source: :user, impl: Phoenix.HTML.FormData.Atom, id: "verify-2fa-auth", name: "user", data: %{}, hidden: [], params: %{}, errors: [], options: [as: nil, id: "verify-2fa-auth", multipart: false, "phx-submit": "verify"], index: nil, action: nil}, :verification}, type: "text"}, errors: [], field: {%Phoenix.HTML.Form{source: :user, impl: Phoenix.HTML.FormData.Atom, id: "verify-2fa-auth", name: "user", data: %{}, hidden: [], params: %{}, errors: [], options: [as: nil, id: "verify-2fa-auth", multipart: false, "phx-submit": "verify"], index: nil, action: nil}, :verification}, id: nil, inner_block: [], label: nil, multiple: false, prompt: nil, rest: %{required: true}, type: "text"}
        (another_test 0.1.0) lib/another_test_web/components/core_components.ex:399: anonymous fn/2 in AnotherTestWeb.CoreComponents."input (overridable 1)"/1
        (another_test 0.1.0) /home/bmorriso/local-repo/livesaastestagain/another_test/lib/another_test_web/live/user_two_factor_live.ex:24: AnotherTestWeb.UserTwoFactorLive.render/1
        (elixir 1.14.3) lib/enum.ex:2468: Enum."-reduce/3-lists^foldl/2-0-"/3
        (phoenix_live_view 0.19.1) lib/phoenix_live_view/diff.ex:383: Phoenix.LiveView.Diff.traverse/7
        (phoenix_live_view 0.19.1) lib/phoenix_live_view/diff.ex:537: anonymous fn/4 in Phoenix.LiveView.Diff.traverse_dynamic/7
        (elixir 1.14.3) lib/enum.ex:2468: Enum."-reduce/3-lists^foldl/2-0-"/3
        (phoenix_live_view 0.19.1) lib/phoenix_live_view/diff.ex:383: Phoenix.LiveView.Diff.traverse/7
        (phoenix_live_view 0.19.1) lib/phoenix_live_view/diff.ex:537: anonymous fn/4 in Phoenix.LiveView.Diff.traverse_dynamic/7
        (elixir 1.14.3) lib/enum.ex:2468: Enum."-reduce/3-lists^foldl/2-0-"/3
        (phoenix_live_view 0.19.1) lib/phoenix_live_view/diff.ex:383: Phoenix.LiveView.Diff.traverse/7
        (phoenix_live_view 0.19.1) lib/phoenix_live_view/diff.ex:537: anonymous fn/4 in Phoenix.LiveView.Diff.traverse_dynamic/7
        (elixir 1.14.3) lib/enum.ex:2468: Enum."-reduce/3-lists^foldl/2-0-"/3
        (phoenix_live_view 0.19.1) lib/phoenix_live_view/diff.ex:383: Phoenix.LiveView.Diff.traverse/7
        (phoenix_live_view 0.19.1) lib/phoenix_live_view/diff.ex:537: anonymous fn/4 in Phoenix.LiveView.Diff.traverse_dynamic/7
        (elixir 1.14.3) lib/enum.ex:2468: Enum."-reduce/3-lists^foldl/2-0-"/3
        (phoenix_live_view 0.19.1) lib/phoenix_live_view/diff.ex:383: Phoenix.LiveView.Diff.traverse/7
        (phoenix_live_view 0.19.1) lib/phoenix_live_view/diff.ex:537: anonymous fn/4 in Phoenix.LiveView.Diff.traverse_dynamic/7
        (elixir 1.14.3) lib/enum.ex:2468: Enum."-reduce/3-lists^foldl/2-0-"/3

The code for this view: (Full source code here defmodule)

      <.simple_form :let={f} for={:user} id="verify-2fa-auth" phx-submit="verify">
        <.input field={{f, :verification}} type="text" required />
        <:actions>
          <.button class="w-full" phx-disable-with="Sending...">Verify</.button>
        </:actions>
      </.simple_form>

The core_components.ex (full source code here)

  def input(assigns) do
    ~H"""
    <div phx-feedback-for={@name} class="space-y-2">
      <.label for={@id}><%= @label %></.label>
      <input
        type={@type}
        name={@name}
        id={@id || @name}
        value={Phoenix.HTML.Form.normalize_value(@type, @value)}
        class={[
          "block w-full p-2.5 bg-gray-50 text-sm rounded-lg border border-gray-300 text-gray-900",
          "focus:ring-4 focus:ring-blue-500/5 focus:border-blue-500",
          "dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500/5 dark:focus:border-blue-500",
          "phx-no-feedback:border-gray-300 phx-no-feedback:focus:border-blue-500 phx-no-feedback:focus:ring-blue-500/5",
          "phx-no-feedback:dark:border-gray-600 phx-no-feedback:dark:focus:border-blue-500 phx-no-feedback:dark:focus:ring-blue-500/5",
          @errors != [] &&
            "text-red-900 placeholder-red-700 border-red-500 focus:ring-red-500 focus:border-red-500 dark:text-red-500 dark:placeholder-red-500 dark:border-red-500"
        ]}
        {@rest}
      />
      <.error :for={msg <- @errors}><%= msg %></.error>
    </div>
    """
  end

Thank you for your time.

Most Liked

kokolegorille

kokolegorille

It should not be necessary to pass the name, as it could be deduced from the field name.

  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)
    |> assign_new(:value, fn -> field.value end)
    |> input()
  end

But You are using an atom… for={:user}

This should not be allowed anymore to pass a single atom.

You should pass an empty changeset, and use for={…} as={:user}

…or set the name attribute manually

  @doc """
  Renders an input with label and error messages.

  A `%Phoenix.HTML.Form{}` and field name may be passed to the input
  to build input names and error messages, or all the attributes and
  errors may be passed explicitly.

  ## Examples

      <.input field={@form[:email]} type="email" />
      <.input name="my-input" errors={["oh no!"]} />
  """
lc0815

lc0815

@kokolegorille I always learn from your code snippets… thanks for sharing

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
Kagamiiiii
Student &amp; New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement