mobilizon.chapril.org-mobil.../lib/web/views/changeset_view.ex
2020-01-26 21:39:49 +01:00

23 lines
630 B
Elixir

defmodule Mobilizon.Web.ChangesetView do
@moduledoc """
View for changesets in case of errors
"""
use Mobilizon.Web, :view
@doc """
Traverses and translates changeset errors.
See `Ecto.Changeset.traverse_errors/2` and
`Mobilizon.Web.ErrorHelpers.translate_error/1` for more details.
"""
def translate_errors(changeset) do
Ecto.Changeset.traverse_errors(changeset, &translate_error/1)
end
def render("error.json", %{changeset: changeset}) do
# When encoded, the changeset returns its errors
# as a JSON object. So we just pass it forward.
%{errors: translate_errors(changeset)}
end
end