mirror of
https://github.com/livebook-dev/livebook.git
synced 2024-11-16 12:57:32 +08:00
30 lines
824 B
Elixir
30 lines
824 B
Elixir
|
defmodule LivebookWeb.ErrorHelpers do
|
||
|
@moduledoc """
|
||
|
Conveniences for translating and building error messages.
|
||
|
"""
|
||
|
use Phoenix.HTML
|
||
|
|
||
|
@doc """
|
||
|
Generates tag for inlined form input errors.
|
||
|
"""
|
||
|
def error_tag(form, field) do
|
||
|
for error <- Keyword.get_values(form.errors, field) do
|
||
|
content_tag(:span, translate_error(error),
|
||
|
class: "invalid-feedback text-red-600",
|
||
|
phx_feedback_for: input_name(form, field)
|
||
|
)
|
||
|
end
|
||
|
end
|
||
|
|
||
|
@doc """
|
||
|
Translates an error message.
|
||
|
"""
|
||
|
def translate_error({msg, opts}) do
|
||
|
# Because the error messages we show in our forms and APIs
|
||
|
# are defined inside Ecto, we need to translate them dynamically.
|
||
|
Enum.reduce(opts, msg, fn {key, value}, acc ->
|
||
|
String.replace(acc, "%{#{key}}", fn _ -> to_string(value) end)
|
||
|
end)
|
||
|
end
|
||
|
end
|