diff --git a/lib/ex_admin/themes/active_admin/form.ex b/lib/ex_admin/themes/active_admin/form.ex index fee7bd9f..13f67c43 100644 --- a/lib/ex_admin/themes/active_admin/form.ex +++ b/lib/ex_admin/themes/active_admin/form.ex @@ -203,8 +203,9 @@ defmodule ExAdmin.Theme.ActiveAdmin.Form do end for field <- fields do - f_name = field[:name] - required = if f_name in required_list, do: true, else: false + f_name = field[:opts][:label] || field[:name] + type = field[:opts][:type] || :text + required = if field[:name] in required_list, do: true, else: false name = "#{base_name}[#{f_name}]" errors = get_errors(errors, String.to_atom("#{field_field_name}_#{orig_inx}_#{f_name}")) error = if errors in [nil, [], false], do: "", else: ".error" @@ -234,7 +235,7 @@ defmodule ExAdmin.Theme.ActiveAdmin.Form do required_abbr required end val = if res, do: [value: Map.get(res, f_name, "") |> escape_value], else: [] - Xain.input([type: :text, maxlength: "255", id: "#{ext_name}_#{f_name}", + Xain.input([type: type, id: "#{ext_name}_#{f_name}", name: name, required: true] ++ val) build_errors(errors, field[:opts][:hint]) end diff --git a/lib/ex_admin/themes/admin_lte2/form.ex b/lib/ex_admin/themes/admin_lte2/form.ex index b77a191e..f7debb7e 100644 --- a/lib/ex_admin/themes/admin_lte2/form.ex +++ b/lib/ex_admin/themes/admin_lte2/form.ex @@ -206,8 +206,9 @@ defmodule ExAdmin.Theme.AdminLte2.Form do end for field <- fields do - f_name = field[:name] - required = if f_name in required_list, do: true, else: false + f_name = field[:opts][:label] || field[:name] + type = field[:opts][:type] || :text + required = if field[:name] in required_list, do: true, else: false name = "#{base_name}[#{f_name}]" errors = get_errors(errors, String.to_atom("#{field_field_name}_#{orig_inx}_#{f_name}")) error = if errors in [nil, [], false], do: "", else: ".has-error" @@ -245,7 +246,7 @@ defmodule ExAdmin.Theme.AdminLte2.Form do required_abbr required end div ".col-sm-10#{error}" do - Xain.input([type: :text, maxlength: "255", id: "#{ext_name}_#{f_name}", + Xain.input([type: type, id: "#{ext_name}_#{f_name}", class: "form-control", name: name] ++ val) build_errors(errors, field[:opts][:hint]) end diff --git a/test/support/migrations.exs b/test/support/migrations.exs index bb6c1a9e..f1924338 100644 --- a/test/support/migrations.exs +++ b/test/support/migrations.exs @@ -68,6 +68,7 @@ defmodule TestExAdmin.Migrations do create table(:phone_numbers) do add :number, :string add :label, :string + add :contacted_on, :date timestamps() end diff --git a/test/support/schema.exs b/test/support/schema.exs index 804a86eb..0976a419 100644 --- a/test/support/schema.exs +++ b/test/support/schema.exs @@ -233,6 +233,7 @@ defmodule TestExAdmin.PhoneNumber do schema "phone_numbers" do field :number, :string field :label, :string + field :contacted_on, :date has_many :contacts_phone_numbers, TestExAdmin.ContactPhoneNumber has_many :contacts, through: [:contacts_phone_numbers, :contact] timestamps() @@ -242,7 +243,7 @@ defmodule TestExAdmin.PhoneNumber do def changeset(model, params \\ %{}) do model - |> cast(params, @fields) + |> cast(params, @fields ++ [:contacted_on]) |> validate_required(@fields) end diff --git a/test/themes/form_test.exs b/test/themes/form_test.exs index 0fd42d46..567b949a 100644 --- a/test/themes/form_test.exs +++ b/test/themes/form_test.exs @@ -23,6 +23,31 @@ defmodule ExAdmin.ThemeFormTest do end + test "AdminLte2 theme_build_has_many_fieldset with labels", %{conn: conn} do + pn = Repo.insert! PhoneNumber.changeset(%PhoneNumber{}, %{label: "Home Phone", number: "5555555555"}) + fields = build_fields(pn, %{label: "Telephone Number"}) + + {inx, html} = AdminLte2.Form.theme_build_has_many_fieldset(conn, pn, fields, 0, "contact_phone_numbers_attributes_0", + :phone_numbers, "phone_numbers_attributes", "contact", nil) + + assert inx == 0 + + assert Floki.find(html, "div div label") |> Floki.text == "Telephone Number*Label*Remove" + end + + test "AdminLte2 theme_build_has_many_fieldset with labels and types", %{conn: conn} do + pn = Repo.insert! PhoneNumber.changeset(%PhoneNumber{}, %{label: "Home Phone", number: "5555555555", contacted_on: Ecto.Date.utc}) + fields = build_fields(pn, %{label: "Telephone Number"}) + contacted_on = %{name: :contacted_on, opts: %{type: :date}, resource: pn, type: :input} + + {inx, html} = AdminLte2.Form.theme_build_has_many_fieldset(conn, pn, [contacted_on | fields], 0, "contact_phone_numbers_attributes_0", + :phone_numbers, "phone_numbers_attributes", "contact", nil) + + assert inx == 0 + + assert Floki.find(html, "div div input") |> Floki.attribute("type") == ["text", "date", "hidden", "checkbox"] + end + test "AdminLte2 theme_build_has_many_fieldset with errors", %{conn: conn} do pn = %{_destroy: "0", label: "Primary Phone", number: nil} fields = [%{name: :label, @@ -44,6 +69,7 @@ defmodule ExAdmin.ThemeFormTest do end + test "ActiveAdmin theme_build_has_many_fieldset", %{conn: conn} do pn = Repo.insert! PhoneNumber.changeset(%PhoneNumber{}, %{label: "Home Phone", number: "5555555555"}) fields = build_fields pn @@ -55,18 +81,44 @@ defmodule ExAdmin.ThemeFormTest do assert Floki.find(html, "fieldset ol h3") |> Floki.text == "Phone Number" end + + test "ActiveAdmin theme_build_has_many_fieldset with labels", %{conn: conn} do + pn = Repo.insert! PhoneNumber.changeset(%PhoneNumber{}, %{label: "Home Phone", number: "5555555555"}) + fields = build_fields(pn, %{label: "Telephone Number"}) + + {inx, html} = ActiveAdmin.Form.theme_build_has_many_fieldset(conn, pn, fields, 0, "contact_phone_numbers_attributes_0", + :phone_numbers, "phone_numbers_attributes", "contact", nil) + + assert inx == 0 + + + assert Floki.find(html, "label") |> Floki.text == "RemoveLabel*Telephone Number*" + end + + test "ActiveAdmin theme_build_has_many_fieldset with labels and types", %{conn: conn} do + pn = Repo.insert! PhoneNumber.changeset(%PhoneNumber{}, %{label: "Home Phone", number: "5555555555", contacted_on: Ecto.Date.utc}) + fields = build_fields(pn, %{label: "Telephone Number"}) + contacted_on = %{name: :contacted_on, opts: %{type: :date}, resource: pn, type: :input} + + {inx, html} = ActiveAdmin.Form.theme_build_has_many_fieldset(conn, pn, [contacted_on | fields], 0, "contact_phone_numbers_attributes_0", + :phone_numbers, "phone_numbers_attributes", "contact", nil) + assert inx == 0 + + assert Floki.find(html, "input") |> Floki.attribute("type") == ["hidden", "checkbox", "date", "text"] + end + ################ # Helpers - defp build_fields(resource) do + defp build_fields(resource, opts \\ %{}) do [ %{ name: :label, resource: resource, type: :input, opts: %{collection: PhoneNumber.labels}, }, - %{name: :number, opts: %{}, resource: resource, type: :input} + %{name: :number, opts: opts, resource: resource, type: :input} ] end end