LiveView & Ecto Association

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 %>
    <div>
      <%= for email_form <- inputs_for(f, :emails) do %>
      <div>
        <%= hidden_inputs_for(email_form) %>
        <%= label email_form, :address, "Email address" %>
        <div><span>Required</span></div>
        <%= email_input email_form, :address, required: true %>
        <%= error_tag email_form, :address %>
      </div>
      <% end %>
      <div>
        <%= label f, :username, "Username" %>
        <div><span>Required</span></div>
        <%= text_input f, :username, required: true %>
        <%= error_tag f, :username %>
      </div>
      <div>
        <%= label f, :fullname, "Full name" %>
        <div><span>Required</span></div>
        <%= text_input f, :fullname, required: true %>
        <%= error_tag f, :fullname %>
      </div>
    </div>
    <div>
      <%# TODO: add the newsletter and the T&C consent %>
    </div>
    <div>
      <div>
        <%= submit "Continue", class: "button button--primary" %>
      </div>
    </div>
    </form>

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} =
        account_live
        |> form("#signup-form", user: @create_attrs)
        |> render_submit()
        |> follow_redirect(conn, Routes.account_verify_path(conn, :verify))
    end

Stacktrace:

1) test Create creates a user with valid attrs (FridayWeb.AccountLive.CreateTest)
     apps/friday_web/test/friday_web/live/account_live/create_test.exs:27
     ** (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()
     stacktrace:
       (phoenix_live_view 0.12.1) lib/phoenix_live_view/test/live_view_test.ex:858: Phoenix.LiveViewTest.call/2
       test/friday_web/live/account_live/create_test.exs:33: (test)

I am not sure what I am doing wrong here.

@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: "some@address.com"}]}

I needed to change mine to look like:

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