cannery/lib/cannery_web/views/error_helpers.ex

85 lines
2.7 KiB
Elixir
Raw Permalink Normal View History

2021-03-11 21:12:55 -05:00
defmodule CanneryWeb.ErrorHelpers do
@moduledoc """
Conveniences for translating and building error messages.
"""
use Phoenix.HTML
import Phoenix.Component
alias Ecto.Changeset
2022-02-08 22:12:22 -05:00
alias Phoenix.{HTML.Form, LiveView.Rendered}
2021-03-11 21:12:55 -05:00
@doc """
Generates tag for inlined form input errors.
"""
@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"""
2023-02-04 10:28:13 -05:00
<span
:for={error <- Keyword.get_values(@form.errors, @field)}
class={["invalid-feedback", @extra_class]}
phx-feedback-for={input_name(@form, @field)}
>
<%= translate_error(error) %>
</span>
"""
2021-03-11 21:12:55 -05:00
end
@doc """
Translates an error message using gettext.
"""
@spec translate_error({String.t(), keyword() | map()}) :: String.t()
2021-03-11 21:12:55 -05:00
def translate_error({msg, opts}) do
# When using gettext, we typically pass the strings we want
# to translate as a static argument:
#
# # Translate "is invalid" in the "errors" domain
# dgettext("errors", "is invalid")
#
# # Translate the number of files with plural rules
# dngettext("errors", "1 file", "%{count} files", count)
#
# Because the error messages we show in our forms and APIs
# are defined inside Ecto, we need to translate them dynamically.
# This requires us to call the Gettext module passing our gettext
# backend as first argument.
#
# Note we use the "errors" domain, which means translations
# should be written to the errors.po file. The :count option is
# set by Ecto and indicates we should also apply plural rules.
if count = opts[:count] do
Gettext.dngettext(CanneryWeb.Gettext, "errors", msg, msg, count, opts)
else
Gettext.dgettext(CanneryWeb.Gettext, "errors", msg, opts)
end
end
@doc """
2022-02-07 23:58:29 -05:00
Displays all errors from a changeset, or just for a single key
"""
@spec changeset_errors(Changeset.t()) :: String.t()
2022-02-07 23:58:29 -05:00
@spec changeset_errors(Changeset.t(), key :: atom()) :: [String.t()] | nil
def changeset_errors(changeset) do
changeset
2022-02-07 19:30:21 -05:00
|> changeset_error_map()
|> Enum.map_join(". ", fn {key, errors} ->
"#{key |> humanize()}: #{errors |> Enum.join(", ")}"
end)
end
2022-02-07 19:30:21 -05:00
2022-02-07 23:58:29 -05:00
def changeset_errors(changeset, key) do
changeset |> changeset_error_map() |> Map.get(key)
end
2022-02-07 19:30:21 -05:00
@doc """
Displays all errors from a changeset in a key value map
"""
@spec changeset_error_map(Changeset.t()) :: %{atom() => [String.t()]}
def changeset_error_map(changeset) do
changeset
|> Changeset.traverse_errors(fn error -> error |> translate_error() end)
end
2021-03-11 21:12:55 -05:00
end