- <%= PhoenixForm.checkbox(
- @form,
- @name,
- class: "checkbox checkbox-sm checkbox-primary",
- checked: selected?(value, @selected),
- name: PhoenixForm.input_name(@form, @name) <> "[]",
- checked_value: value,
- value: value,
- hidden_input: false
- ) %>
+
"[]"}
+ class="checkbox checkbox-sm checkbox-primary"
+ checked={selected?(value, @selected)}
+ checked_value={value}
+ value={value}
+ />
<%= label %>
@@ -270,49 +353,21 @@ defmodule Backpex.HTML.Form do
"""
end
- defp selected?(id, selected), do: Enum.any?(selected, fn {_label, value} -> id == value end)
+ def form_errors?(false, _form), do: false
+ def form_errors?(true = _show_errors, form), do: form.errors != []
- @doc """
- Generates form field input errors.
- """
- @doc type: :component
- attr(:form, :any, required: true, doc: "form the error tag should be for")
- attr(:name, :atom, required: true, doc: "the name of the field")
- attr(:field_options, :map, required: true, doc: "the field options")
+ defp translate_form_errors(form_field, field_options) do
+ translator_func =
+ case field_options do
+ %{translate_error: translate_error} = _field_options ->
+ fn error -> translate_error.(error) end
- def error_tag(assigns) do
- assigns =
- assigns
- |> assign(:translated_errors, translated_form_errors(assigns.form, assigns.name, assigns.field_options))
+ _field_options ->
+ &Function.identity/1
+ end
- ~H"""
-
- <%= translated_error %>
-
- """
- end
-
- def form_errors?(false, _changeset), do: false
-
- def form_errors?(true = _show_errors, changeset) do
- Ecto.Changeset.traverse_errors(changeset, & &1)
- |> Enum.count() > 0
+ Enum.map(form_field.errors, fn error -> translator_func.(error) |> elem(0) end)
end
- defp translated_form_errors(form, name, field_options) do
- Keyword.get_values(form.errors || [], name)
- |> Enum.reduce([], fn el, acc ->
- el = translate_error(el, field_options)
-
- Keyword.merge(acc, [el])
- end)
- |> Enum.map(&Backpex.translate(&1, :error))
- end
-
- defp translate_error(error, %{translate_error: translate_error} = _field_options), do: translate_error.(error)
- defp translate_error(error, _field_options), do: error
+ defp selected?(id, selected), do: Enum.any?(selected, fn {_label, value} -> id == value end)
end
diff --git a/lib/backpex/html/resource.ex b/lib/backpex/html/resource.ex
index 23ea75ff..d1862d79 100644
--- a/lib/backpex/html/resource.ex
+++ b/lib/backpex/html/resource.ex
@@ -182,20 +182,23 @@ defmodule Backpex.HTML.Resource do
attr(:placeholder, :string, required: true, doc: "placeholder for the search input")
def index_search_form(assigns) do
+ form = to_form(%{"value" => assigns.value}, as: :index_search)
+ search_enabled = not is_nil(assigns.full_text_search) or assigns.searchable_fields != []
+
assigns =
assigns
- |> assign(:search_enabled, not is_nil(assigns.full_text_search) or assigns.searchable_fields != [])
+ |> assign(:search_enabled, search_enabled)
+ |> assign(:form, form)
~H"""
- <.form :let={f} :if={@search_enabled} for={%{}} as={:index_search} phx-change="index-search" phx-submit="index-search">
- <%= Phoenix.HTML.Form.search_input(
- f,
- :search_input,
- class: "input input-sm input-bordered",
- placeholder: @placeholder,
- phx_debounce: "200",
- value: @value
- ) %>
+ <.form :if={@search_enabled} for={@form} phx-change="index-search" phx-submit="index-search">
+
"""
end
@@ -275,7 +278,7 @@ defmodule Backpex.HTML.Resource do
<% value = Map.get(@filter_options, Atom.to_string(field), nil) %>
- <.form :let={f} for={%{}} as={:filters} phx-change="change-filter" phx-submit="change-filter">
+ <.form :let={f} for={to_form(%{}, as: :filters)} phx-change="change-filter" phx-submit="change-filter">
<%= filter.module.label() %>
@@ -338,6 +341,13 @@ defmodule Backpex.HTML.Resource do
attr(:x_style, :string, default: "", doc: "alpine-bound inline styles for the root div")
def toggle_columns(assigns) do
+ form =
+ to_form(%{"_resource" => assigns.live_resource, "_cookie_redirect_url" => assigns.current_url},
+ as: :toggle_columns
+ )
+
+ assigns = assign(assigns, :form, form)
+
~H"""
@@ -347,10 +357,10 @@ defmodule Backpex.HTML.Resource do
- <.form :let={f} method="POST" for={%{}} as={:toggle_columns} action={cookie_path(@socket)}>
- <%= Phoenix.HTML.Form.hidden_input(f, :_resource, value: @live_resource) %>
- <%= Phoenix.HTML.Form.hidden_input(f, :_cookie_redirect_url, value: @current_url) %>
- <.toggle_columns_inputs active_fields={@active_fields} form={f} />
+ <.form method="POST" for={@form} action={cookie_path(@socket)}>
+
+
+ <.toggle_columns_inputs active_fields={@active_fields} form={@form} />
<%= Backpex.translate("Save") %>
@@ -368,13 +378,8 @@ defmodule Backpex.HTML.Resource do
- <%= Phoenix.HTML.Form.checkbox(
- @form,
- name,
- id: name,
- class: "checkbox checkbox-sm checkbox-primary",
- checked: active
- ) %>
+
+
<%= label %>
@@ -610,22 +615,16 @@ defmodule Backpex.HTML.Resource do
attr(:class, :string, default: "", doc: "Extra class to be added to the select.")
def select_per_page(assigns) do
+ form = to_form(%{}, as: :select_per_page)
+
+ assigns =
+ assigns
+ |> assign(:form, form)
+ |> assign(:selected, assigns.query_options.per_page)
+
~H"""
- <.form
- :let={f}
- for={%{}}
- as={:select_per_page}
- class={@class}
- phx-change="select-page-size"
- phx-submit="select-page-size"
- >
- <%= Phoenix.HTML.Form.select(
- f,
- :per_page,
- @options,
- class: "select select-sm select-bordered",
- selected: @query_options.per_page
- ) %>
+ <.form for={@form} class={@class} phx-change="select-page-size" phx-submit="select-page-size">
+
"""
end
@@ -961,13 +960,20 @@ defmodule Backpex.HTML.Resource do
defp metric_toggle(assigns) do
visible = Backpex.Metric.metrics_visible?(assigns.metric_visibility, assigns.live_resource)
- assigns = assign(assigns, :visible, visible)
+ form =
+ %{"_resource" => assigns.live_resource, "_cookie_redirect_url" => assigns.current_url}
+ |> to_form(as: :toggle_metrics)
+
+ assigns =
+ assigns
+ |> assign(:visible, visible)
+ |> assign(:form, form)
~H"""
0}>
- <.form :let={f} method="POST" for={%{}} as={:toggle_metrics} action={cookie_path(@socket)}>
- <%= Phoenix.HTML.Form.hidden_input(f, :_resource, value: @live_resource) %>
- <%= Phoenix.HTML.Form.hidden_input(f, :_cookie_redirect_url, value: @current_url) %>
+ <.form method="POST" for={@form} action={cookie_path(@socket)}>
+
+