2021-03-11 21:12:55 -05:00
|
|
|
defmodule CanneryWeb.ErrorHelpers do
|
|
|
|
@moduledoc """
|
|
|
|
Conveniences for translating and building error messages.
|
|
|
|
"""
|
|
|
|
|
|
|
|
use Phoenix.HTML
|
2022-11-07 22:36:38 -05:00
|
|
|
import Phoenix.Component
|
2022-02-05 23:41:10 -05:00
|
|
|
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.
|
|
|
|
"""
|
2022-02-05 23:41:10 -05:00
|
|
|
@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"""
|
2022-02-07 19:30:21 -05:00
|
|
|
<%= for error <- Keyword.get_values(@form.errors, @field) do %>
|
2023-01-25 23:13:16 -05:00
|
|
|
<span class={["invalid-feedback", @extra_class]} phx-feedback-for={input_name(@form, @field)}>
|
2022-02-07 19:30:21 -05:00
|
|
|
<%= translate_error(error) %>
|
|
|
|
</span>
|
|
|
|
<% end %>
|
2022-02-05 23:41:10 -05:00
|
|
|
"""
|
2021-03-11 21:12:55 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Translates an error message using gettext.
|
|
|
|
"""
|
2022-02-05 23:41:10 -05:00
|
|
|
@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
|
2022-02-05 23:41:10 -05:00
|
|
|
|
|
|
|
@doc """
|
2022-02-07 23:58:29 -05:00
|
|
|
Displays all errors from a changeset, or just for a single key
|
2022-02-05 23:41:10 -05:00
|
|
|
"""
|
|
|
|
@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
|
2022-02-05 23:41:10 -05:00
|
|
|
def changeset_errors(changeset) do
|
|
|
|
changeset
|
2022-02-07 19:30:21 -05:00
|
|
|
|> changeset_error_map()
|
2022-02-05 23:41:10 -05:00
|
|
|
|> 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
|