From 033d07a519bac03fdfd28ca15e09cc287e80fd14 Mon Sep 17 00:00:00 2001 From: neodarz Date: Tue, 24 Jan 2017 17:09:18 +0100 Subject: Initial commit with non-functional pieces of code about file upload --- web/views/error_helpers.ex | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 web/views/error_helpers.ex (limited to 'web/views/error_helpers.ex') diff --git a/web/views/error_helpers.ex b/web/views/error_helpers.ex new file mode 100644 index 0000000..6f77aa4 --- /dev/null +++ b/web/views/error_helpers.ex @@ -0,0 +1,40 @@ +defmodule TheTranscriberBackend.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 + if error = form.errors[field] do + content_tag :span, translate_error(error), class: "help-block" + end + end + + @doc """ + Translates an error message using gettext. + """ + def translate_error({msg, opts}) do + # Because error messages were defined within Ecto, we must + # call the Gettext module passing our Gettext backend. We + # also use the "errors" domain as translations are placed + # in the errors.po file. + # Ecto will pass the :count keyword if the error message is + # meant to be pluralized. + # On your own code and templates, depending on whether you + # need the message to be pluralized or not, this could be + # written simply as: + # + # dngettext "errors", "1 file", "%{count} files", count + # dgettext "errors", "is invalid" + # + if count = opts[:count] do + Gettext.dngettext(TheTranscriberBackend.Gettext, "errors", msg, msg, count, opts) + else + Gettext.dgettext(TheTranscriberBackend.Gettext, "errors", msg, opts) + end + end +end -- cgit v1.2.1