Form submission - some field names are missing

I have this below form

<%= form_for @conn, Routes.admin_user_path(@conn, :create), [as: :create_user, id: "user-form", 
    class: "mt-5 mb-5 login-input", method: :post], fn f -> %>

        <div class="form-group">
            <%= text_input f, :name, placeholder: "Name", class: "form-control" %>
        </div>
        <div class="form-group">
            <%= text_input f, :email, placeholder: "Email", class: "form-control" %>
        </div>
        <div class="form-group">
            <%= text_input f, :password, placeholder: "Password", class: "form-control" %>

        </div>

        <h4 class="card-title">Select Allowed Permission Groups</h4>

        <div class="form-group mx-sm-3 mb-2">

          <%= label f, "Custom fields" %>

              <div class="checkbox">
                <label><input type="checkbox"  name="fields[]"  
                    value="1" /> admin</label>
              </div>
              <div class="checkbox">
                <label><input type="checkbox"  name="fields[]" 
                    value="2" />admin 2</label>
              </div>

        </div>

    <br/>
    <input type="submit" value="Save" class="btn btn-dark mb-2" />
<%= end %>

In my controller

  def create(conn, %{"create_user" => user_params}) do

    IO.inspect user_params
    
    case Admins.create_user(user_params) do
      {:ok, user} ->
        conn
        |> put_flash(:info, "User created successfully.")
        |> redirect(to: Routes.admin_user_path(conn, :show, user))

      {:error, %Ecto.Changeset{} = changeset} ->
        render(conn, "new.html", changeset: changeset)
    end
  end

I am printing the user_params to check what are the fields are coming. Below is what i am getting

%{“email” => “test@test.com”, “name” => “test”, “password” => “12345678”}

My checkbox fields[] params are missing

But in my terminal, I am seeing that fields param.

[info] POST /admin/user/add
[debug] Processing with LogisticsWeb.Admin.UserController.create/2
  Parameters: %{"_csrf_token" => "Ai8DNX57AloiHl19bzVcFCxGbxNSWCJFsxpj1IH2dGdNBE8lY79B90Rq", "_utf8" => "✓", "create_user" => %{"email" => "", "name" => "", "password" => "[FILTERED]"}, "fields" => ["1", "2"]}
  Pipelines: [:browser, :admin, LogisticsWeb.Plugs.AuthenticateAdmin]

Like it is coming like this

%{
  "_csrf_token" => "QR44HR0FDRkxHnRLYhQXTjIYAWMdHR1/0IKBR7GqwGMxOds6GiW2vumK",
  "_utf8" => "✓",
  "create_user" => %{"email" => "", "name" => "", "password" => ""},
  "fields" => ["1", "2"]
}

why the fields param is not inside the create_user map?

Am I missing something?

You need to extract the fields in your match by the looks of things.

Edit: turns out you can’t highlight text in code, at least not on an iPhone.

1 Like

Yeah, this is what I have done it, but I would like to know why that the “field” map is not coming in the “create_user” map.
Any idea @mindok?

The name you have set in your template (for the checkboxes)?

<input type="checkbox" name="fields[]" value="1" />

It should be…

<input type="checkbox" name="create_user[fields][]" value="1" />
3 Likes