LiveView & Ecto Association issue: (ArgumentError) could not find non-disabled input

Hello all!

I am trying to create a user registration form using LiveView but I am running into an issue with one of the field. My User model has an association with the Email model which defines that a user can have many emails.

I setup the following form

<%= f = form_for @changeset, "#",
      id: "signup-form",
      phx_change: "validate",
      phx_submit: "submit",
      as: :user %>
      <%= for email_form <- inputs_for(f, :emails) do %>
        <%= hidden_inputs_for(email_form) %>
        <%= label email_form, :address, "Email address" %>
        <%= email_input email_form, :address, required: true %>
        <%= error_tag email_form, :address %>
      <% end %>
        <%= label f, :username, "Username" %>
        <%= text_input f, :username, required: true %>
        <%= error_tag f, :username %>
        <%= label f, :fullname, "Full name" %>
        <%= text_input f, :fullname, required: true %>
        <%= error_tag f, :fullname %>
      <%# TODO: add the newsletter and the T&C consent %>
        <%= submit "Continue", class: "button button--primary" %>

When testing in the browser, the user gets created properly in the DB but my ExUnit test is failing. I setup the following test:

test "creates a user with valid attrs", %{conn: conn} do
      {:ok, account_live, _html} = account_create_path(conn)

      {:ok, _, html} =
        |> form("#signup-form", user: @create_attrs)
        |> render_submit()
        |> follow_redirect(conn, Routes.account_verify_path(conn, :verify))


1) test Create creates a user with valid attrs (FridayWeb.AccountLive.CreateTest)
     ** (ArgumentError) could not find non-disabled input, select or textarea with name "user[emails][]" within:

         <input name="_csrf_token" type="hidden" value="AjsAPT5XVQgPBA0tJiw7Hnx8Jj5ZFCo8oCUhf6lWWBlCDcW_0MEI7uRL"/>
         <input id="user_emails_0_address" name="user[emails][0][address]" type="email" required="required"/>
         <input id="user_username" name="user[username]" type="text" required="required"/>
         <input id="user_fullname" name="user[fullname]" type="text" required="required"/>

     code: |> render_submit()
       (phoenix_live_view 0.12.1) lib/phoenix_live_view/test/live_view_test.ex:858:
       test/friday_web/live/account_live/create_test.exs:33: (test)

I am not sure what I am doing wrong here.

1 Like

@makabde, I don’t know if you are still having this problem or not. I was running into the same problem, was not able to find a solution online, but stumbled my way through to something that worked.

I’m assuming your @create_attrs look something like:

@create_attrs %{emails: [%{address: ""}]}

I needed to change mine to look like:

@create_attrs %{emails: %{0 => %{address: ""}}}

I’ve been struggling for one hour with this! Thank you so much, you made my day ;-).