Skip to content

Instantly share code, notes, and snippets.

@iamvery
Created March 28, 2018 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamvery/d2429c6ab5fc7f32d7fafe3af167ba29 to your computer and use it in GitHub Desktop.
Save iamvery/d2429c6ab5fc7f32d7fafe3af167ba29 to your computer and use it in GitHub Desktop.
defmodule PantsWeb.ChangesetView do
use PantsWeb, :view
@doc """
Traverses and translates changeset errors.
See `Ecto.Changeset.traverse_errors/2` and
`PantsWeb.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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment