diff --git a/lib/cannery_web/live/ammo_group_live/form_component.ex b/lib/cannery_web/live/ammo_group_live/form_component.ex
index 46e28b2..e2a625f 100644
--- a/lib/cannery_web/live/ammo_group_live/form_component.ex
+++ b/lib/cannery_web/live/ammo_group_live/form_component.ex
@@ -23,12 +23,7 @@ defmodule CanneryWeb.AmmoGroupLive.FormComponent do
@impl true
def handle_event("validate", %{"ammo_group" => ammo_group_params}, socket) do
ammo_group_params = ammo_group_params |> Map.put("user_id", socket.assigns.current_user.id)
-
- changeset =
- socket.assigns.ammo_group
- |> Ammo.change_ammo_group(ammo_group_params)
- |> Map.put(:action, :validate)
-
+ changeset = socket.assigns.ammo_group |> Ammo.change_ammo_group(ammo_group_params)
{:noreply, socket |> assign(:changeset, changeset)}
end
@@ -53,48 +48,44 @@ defmodule CanneryWeb.AmmoGroupLive.FormComponent do
phx-submit="save"
class="grid grid-cols-3 justify-center items-center space-y-4"
>
+ <%= if @changeset.action do %>
+
-
- <%= error_tag(f, :bullet_type) %>
-
+ <%= error_tag(f, :bullet_type, "col-span-3 text-center") %>
<%= label(f, :bullet_core, class: "mr-4 title text-lg text-primary-500") %>
<%= text_input(f, :bullet_core,
class: "text-center col-span-2 input input-primary",
placeholder: "Steel"
) %>
-
- <%= error_tag(f, :bullet_core) %>
-
+ <%= error_tag(f, :bullet_core, "col-span-3 text-center") %>
<%= label(f, :cartridge, class: "mr-4 title text-lg text-primary-500") %>
<%= text_input(f, :cartridge,
class: "text-center col-span-2 input input-primary",
placeholder: "5.56x46mm NATO"
) %>
-
- <%= error_tag(f, :cartridge) %>
-
+ <%= error_tag(f, :cartridge, "col-span-3 text-center") %>
<%= label(f, :caliber, class: "mr-4 title text-lg text-primary-500") %>
<%= text_input(f, :caliber,
class: "text-center col-span-2 input input-primary",
placeholder: ".223"
) %>
-
- <%= error_tag(f, :caliber) %>
-
+ <%= error_tag(f, :caliber, "col-span-3 text-center") %>
<%= label(f, :case_material, class: "mr-4 title text-lg text-primary-500") %>
<%= text_input(f, :case_material,
class: "text-center col-span-2 input input-primary",
placeholder: "Brass"
) %>
-
- <%= error_tag(f, :case_material) %>
-
+ <%= error_tag(f, :case_material, "col-span-3 text-center") %>
<%= label(f, :grains, class: "mr-4 title text-lg text-primary-500") %>
<%= number_input(f, :grains,
@@ -120,60 +108,42 @@ defmodule CanneryWeb.AmmoTypeLive.FormComponent do
class: "text-center col-span-2 input input-primary",
min: 1
) %>
-
- <%= error_tag(f, :grains) %>
-
+ <%= error_tag(f, :grains, "col-span-3 text-center") %>
<%= label(f, :pressure, class: "mr-4 title text-lg text-primary-500") %>
<%= text_input(f, :pressure,
class: "text-center col-span-2 input input-primary",
placeholder: "+P"
) %>
-
- <%= error_tag(f, :pressure) %>
-
+ <%= error_tag(f, :pressure, "col-span-3 text-center") %>
<%= label(f, :rimfire, class: "mr-4 title text-lg text-primary-500") %>
<%= checkbox(f, :rimfire, class: "text-center col-span-2 checkbox") %>
-
- <%= error_tag(f, :rimfire) %>
-
+ <%= error_tag(f, :rimfire, "col-span-3 text-center") %>
<%= label(f, :tracer, class: "mr-4 title text-lg text-primary-500") %>
<%= checkbox(f, :tracer, class: "text-center col-span-2 checkbox") %>
-
- <%= error_tag(f, :tracer) %>
-
+ <%= error_tag(f, :tracer, "col-span-3 text-center") %>
<%= label(f, :incendiary, class: "mr-4 title text-lg text-primary-500") %>
<%= checkbox(f, :incendiary, class: "text-center col-span-2 checkbox") %>
-
- <%= error_tag(f, :incendiary) %>
-
+ <%= error_tag(f, :incendiary, "col-span-3 text-center") %>
<%= label(f, :blank, class: "mr-4 title text-lg text-primary-500") %>
<%= checkbox(f, :blank, class: "text-center col-span-2 checkbox") %>
-
- <%= error_tag(f, :blank) %>
-
+ <%= error_tag(f, :blank, "col-span-3 text-center") %>
<%= label(f, :corrosive, class: "mr-4 title text-lg text-primary-500") %>
<%= checkbox(f, :corrosive, class: "text-center col-span-2 checkbox") %>
-
- <%= error_tag(f, :corrosive) %>
-
+ <%= error_tag(f, :corrosive, "col-span-3 text-center") %>
<%= label(f, :manufacturer, class: "mr-4 title text-lg text-primary-500") %>
<%= text_input(f, :manufacturer, class: "text-center col-span-2 input input-primary") %>
-
- <%= error_tag(f, :manufacturer) %>
-
+ <%= error_tag(f, :manufacturer, "col-span-3 text-center") %>
<%= label(f, :sku, class: "mr-4 title text-lg text-primary-500") %>
<%= text_input(f, :sku, class: "text-center col-span-2 input input-primary") %>
-
- <%= error_tag(f, :sku) %>
-
+ <%= error_tag(f, :sku, "col-span-3 text-center") %>
<%= submit("Save",
phx_disable_with: "Saving...",
diff --git a/lib/cannery_web/live/container_live/form_component.ex b/lib/cannery_web/live/container_live/form_component.ex
index fa1f1a0..6342c59 100644
--- a/lib/cannery_web/live/container_live/form_component.ex
+++ b/lib/cannery_web/live/container_live/form_component.ex
@@ -17,12 +17,7 @@ defmodule CanneryWeb.ContainerLive.FormComponent do
@impl true
def handle_event("validate", %{"container" => container_params}, socket) do
container_params = container_params |> Map.put("user_id", socket.assigns.current_user.id)
-
- changeset =
- socket.assigns.container
- |> Containers.change_container(container_params)
- |> Map.put(:action, :validate)
-
+ changeset = socket.assigns.container |> Containers.change_container(container_params)
{:noreply, socket |> assign(:changeset, changeset)}
end
@@ -47,40 +42,42 @@ defmodule CanneryWeb.ContainerLive.FormComponent do
phx-change="validate"
phx-submit="save"
>
+ <%= if @changeset.action do %>
+
+ <%= changeset_errors(@changeset) %>
+
+ <% end %>
+
<%= label(f, :name, class: "title text-lg text-primary-500") %>
<%= text_input(f, :name,
class: "input input-primary col-span-2",
placeholder: "My cool ammo can"
) %>
-
- <%= error_tag(f, :name) %>
-
+ <%= error_tag(f, :name, "col-span-3 text-center") %>
+
<%= label(f, :desc, class: "title text-lg text-primary-500") %>
<%= textarea(f, :desc,
class: "input input-primary col-span-2",
phx_hook: "MaintainAttrs",
placeholder: "Metal ammo can with the anime girl sticker"
) %>
-
- <%= error_tag(f, :desc) %>
-
+ <%= error_tag(f, :desc, "col-span-3 text-center") %>
+
<%= label(f, :type, class: "title text-lg text-primary-500") %>
<%= text_input(f, :type,
class: "input input-primary col-span-2",
placeholder: "Magazine, Clip, Ammo Box, etc"
) %>
-
- <%= error_tag(f, :type) %>
-
+ <%= error_tag(f, :type, "col-span-3 text-center") %>
+
<%= label(f, :location, class: "title text-lg text-primary-500") %>
<%= textarea(f, :location,
class: "input input-primary col-span-2",
phx_hook: "MaintainAttrs",
placeholder: "On the bookshelf"
) %>
-
- <%= error_tag(f, :location) %>
-
+ <%= error_tag(f, :location, "col-span-3 text-center") %>
+
<%= submit("Save",
class: "mx-auto btn btn-primary col-span-3",
phx_disable_with: "Saving..."
diff --git a/lib/cannery_web/live/invite_live/form_component.ex b/lib/cannery_web/live/invite_live/form_component.ex
index 364e544..32385c5 100644
--- a/lib/cannery_web/live/invite_live/form_component.ex
+++ b/lib/cannery_web/live/invite_live/form_component.ex
@@ -20,11 +20,7 @@ defmodule CanneryWeb.InviteLive.FormComponent do
@impl true
def handle_event("validate", %{"invite" => invite_params}, socket) do
- changeset =
- socket.assigns.invite
- |> Invites.change_invite(invite_params)
- |> Map.put(:action, :validate)
-
+ changeset = socket.assigns.invite |> Invites.change_invite(invite_params)
{:noreply, assign(socket, :changeset, changeset)}
end
@@ -48,16 +44,20 @@ defmodule CanneryWeb.InviteLive.FormComponent do
phx-change="validate"
phx-submit="save"
>
+ <%= if @changeset.action do %>
+
+ <%= changeset_errors(@changeset) %>
+
+ <% end %>
+
<%= label(f, :name, class: "title text-lg text-primary-500") %>
<%= text_input(f, :name, class: "input input-primary col-span-2") %>
-
- <%= error_tag(f, :name) %>
-
+ <%= error_tag(f, :name, "col-span-3") %>
+
<%= label(f, :uses_left, class: "title text-lg text-primary-500") %>
<%= number_input(f, :uses_left, min: 0, class: "input input-primary col-span-2") %>
-
- <%= error_tag(f, :uses_left) %>
-
+ <%= error_tag(f, :uses_left, "col-span-3") %>
+
<%= submit("Save",
class: "mx-auto btn btn-primary col-span-3",
phx_disable_with: "Saving..."
diff --git a/lib/cannery_web/live/tag_live/form_component.ex b/lib/cannery_web/live/tag_live/form_component.ex
index ca39705..9dbc1dd 100644
--- a/lib/cannery_web/live/tag_live/form_component.ex
+++ b/lib/cannery_web/live/tag_live/form_component.ex
@@ -22,10 +22,7 @@ defmodule CanneryWeb.TagLive.FormComponent do
def handle_event("validate", %{"tag" => tag_params}, socket) do
tag_params = tag_params |> Map.put("user_id", socket.assigns.current_user.id)
- changeset =
- socket.assigns.tag
- |> Tags.change_tag(tag_params)
- |> Map.put(:action, :validate)
+ changeset = socket.assigns.tag |> Tags.change_tag(tag_params)
{:noreply, socket |> assign(:changeset, changeset)}
end
@@ -51,25 +48,28 @@ defmodule CanneryWeb.TagLive.FormComponent do
phx-change="validate"
phx-submit="save"
>
+ <%= if @changeset.action do %>
+
+ <%= changeset_errors(@changeset) %>
+
+ <% end %>
+
<%= label(f, :name, class: "title text-lg text-primary-500") %>
<%= text_input(f, :name, class: "input input-primary col-span-2") %>
-
- <%= error_tag(f, :name) %>
-
+ <%= error_tag(f, :name, "col-span-3") %>
+
<%= label(f, :bg_color, class: "title text-lg text-primary-500") %>
<%= color_input(f, :bg_color) %>
-
- <%= error_tag(f, :bg_color) %>
-
+ <%= error_tag(f, :bg_color, "col-span-3") %>
+
<%= label(f, :text_color, class: "title text-lg text-primary-500") %>
<%= color_input(f, :text_color) %>
-
- <%= error_tag(f, :text_color) %>
-
+ <%= error_tag(f, :text_color, "col-span-3") %>
+
<%= submit("Save",
class: "mx-auto btn btn-primary col-span-3",
phx_disable_with: "Saving..."
diff --git a/lib/cannery_web/views/error_helpers.ex b/lib/cannery_web/views/error_helpers.ex
index 58d349c..6fce063 100644
--- a/lib/cannery_web/views/error_helpers.ex
+++ b/lib/cannery_web/views/error_helpers.ex
@@ -4,22 +4,32 @@ defmodule CanneryWeb.ErrorHelpers do
"""
use Phoenix.HTML
+ import Phoenix.LiveView.Helpers
+ alias Phoenix.{HTML.Form, LiveView.Rendered}
+ alias Ecto.Changeset
@doc """
Generates tag for inlined form input errors.
"""
- def error_tag(form, field) do
- Enum.map(Keyword.get_values(form.errors, field), fn error ->
- content_tag(:span, translate_error(error),
- class: "invalid-feedback",
- phx_feedback_for: input_name(form, field)
- )
- end)
+ @spec error_tag(Form.t(), Form.field()) :: Rendered.t()
+ @spec error_tag(Form.t(), Form.field(), String.t()) :: Rendered.t()
+ def error_tag(form, field, extra_class \\ "") do
+ assigns = %{extra_class: extra_class, form: form, field: field}
+
+ ~H"""
+ <%= for error <- Keyword.get_values(@form.errors, @field) do %>
+
+ <%= translate_error(error) %>
+
+ <% end %>
+ """
end
@doc """
Translates an error message using gettext.
"""
+ @spec translate_error({String.t(), keyword() | map()}) :: String.t()
def translate_error({msg, opts}) do
# When using gettext, we typically pass the strings we want
# to translate as a static argument:
@@ -44,4 +54,16 @@ defmodule CanneryWeb.ErrorHelpers do
Gettext.dgettext(CanneryWeb.Gettext, "errors", msg, opts)
end
end
+
+ @doc """
+ Displays all errors from a changeset
+ """
+ @spec changeset_errors(Changeset.t()) :: String.t()
+ def changeset_errors(changeset) do
+ changeset
+ |> Changeset.traverse_errors(fn error -> error |> translate_error() end)
+ |> Enum.map_join(". ", fn {key, errors} ->
+ "#{key |> humanize()}: #{errors |> Enum.join(", ")}"
+ end)
+ end
end