Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ function that handles it:

```elixir
config :phoenix_bootstrap_form, [
translate_error_function: &MyApp.ErrorHelpers.translate_error/1
translate_error_function: {MyApp.ErrorHelpers, :translate_error}
]
```

Expand Down
22 changes: 13 additions & 9 deletions lib/phoenix_bootstrap_form.ex
Original file line number Diff line number Diff line change
Expand Up @@ -357,16 +357,20 @@ defmodule PhoenixBootstrapForm do
Tag.content_tag(:div, message, class: "invalid-feedback")
end

defp translate_error(msg, opts) do
default_fn = fn {msg, opts} ->
Enum.reduce(opts, msg, fn {key, value}, acc ->
String.replace(acc, "%{#{key}}", to_string(value))
end)
end
def default_translate_error({msg, opts}) do
Enum.reduce(opts, msg, fn {key, value}, acc ->
String.replace(acc, "%{#{key}}", to_string(value))
end)
end

translate_error_fn =
Application.get_env(:phoenix_bootstrap_form, :translate_error_function, default_fn)
defp translate_error(msg, opts) do
{module, function} =
Application.get_env(
:phoenix_bootstrap_form,
:translate_error_function,
{__MODULE__, :default_translate_error}
)

translate_error_fn.({msg, opts})
apply(module, function, [{msg, opts}])
end
end
28 changes: 18 additions & 10 deletions test/phoenix_bootstrap_form_error_helper_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,33 @@ defmodule PhoenixBootstrapFormErrorHelperTest do

doctest PhoenixBootstrapForm

def custom_translate_error({msg, opts}) do
"#{msg}, #{opts[:count]}"
end

setup do
Application.put_env(:phoenix_bootstrap_form, :translate_error_function, fn {msg, opts} ->
"#{msg}, #{opts[:count]}"
end)
Application.put_env(
:phoenix_bootstrap_form,
:translate_error_function,
{__MODULE__, :custom_translate_error}
)

conn = Phoenix.ConnTest.build_conn()
form = Phoenix.HTML.FormData.to_form(conn, [as: :record, multipart: true])
form = Phoenix.HTML.FormData.to_form(conn, as: :record, multipart: true)
{:ok, [conn: conn, form: form]}
end

test "custom error helper", %{form: form} do
error = [value: {"Got errors - %{count}", [count: 10]}]
form = %Phoenix.HTML.Form{form | errors: error}
input = PhoenixBootstrapForm.text_input(form, :value)

assert safe_to_string(input) ==
~s(<div class="form-group row">) <>
~s(<label class="col-form-label text-sm-right col-sm-2" for="record_value">Value</label>) <>
~s(<div class="col-sm-10">) <>
~s(<input class="form-control is-invalid" id="record_value" name="record[value]" type="text">) <>
~s(<div class="invalid-feedback">Got errors - %{count}, 10</div>) <>
~s(</div></div>)
~s(<div class="form-group row">) <>
~s(<label class="col-form-label text-sm-right col-sm-2" for="record_value">Value</label>) <>
~s(<div class="col-sm-10">) <>
~s(<input class="form-control is-invalid" id="record_value" name="record[value]" type="text">) <>
~s(<div class="invalid-feedback">Got errors - %{count}, 10</div>) <>
~s(</div></div>)
end
end