2020-01-26 21:36:50 +01:00
|
|
|
defmodule Mobilizon.Web.ChangesetView do
|
2018-01-14 17:56:50 +01:00
|
|
|
@moduledoc """
|
|
|
|
View for changesets in case of errors
|
|
|
|
"""
|
2020-01-26 21:36:50 +01:00
|
|
|
use Mobilizon.Web, :view
|
2018-01-13 23:33:03 +01:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Traverses and translates changeset errors.
|
|
|
|
|
|
|
|
See `Ecto.Changeset.traverse_errors/2` and
|
2020-01-26 21:36:50 +01:00
|
|
|
`Mobilizon.Web.ErrorHelpers.translate_error/1` for more details.
|
2018-01-13 23:33:03 +01:00
|
|
|
"""
|
|
|
|
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
|