How to add a custom error message for a required field in Phoenix infrastructure

How can I change the error message for required fields? If I have something like this

@required_fields ~w(name email) 

and I want to show "no empty" instead of the default value "cannot be empty"?

+5
source share
3 answers

The "can't be blank" error message is currently hardcoded in Ecto. However, you can replace this error message by doing:

 def changeset(model, params \\ :empty) do model |> cast(params, @required_fields, @optional_fields) |> required_error_messages("no way it empty") end def required_error_messages(changeset, new_error_message) do update_in changeset.errors, &Enum.map(&1, fn {key, "can't be blank"} -> {key, new_error_message} {_key, _error} = tuple -> tuple end) end 

Hope this helps!

+7
source

I usually configure this method:

 validate_required(changeset, [:email], message: "Email cannot be blank.") 
+5
source

I think Ecto.Changeset may have changed since Ecto.Changeset last answer. Starting with ecto_sql 3.1, the %Ecto.Changeset{} structure stores the following errors:

 errors: [address1: {"can't be blank", [validation: :required]}] 

Therefore, I had to slightly modify the structure of the previous solution. In this example, I use cast / 4 to cast the raw map (the first argument can be a set of changes or a tuple of data like {data, types} ):

 @permitted [:name, :phone, :url] @parameter_types %{name: :string, phone: :string, url: :string} def signup_changeset(params) do IO.inspect params cast({%{}, @parameter_types}, params, @permitted) |> validate_required([:name, :phone, :url]) |> required_error_messages("no way it empty") end defp required_error_messages(changeset, new_error_message) do update_in changeset.errors, &Enum.map(&1, fn {key, {"can't be blank", rules}} -> {key, {new_error_message, rules}} tuple -> tuple end) end 

Note that you need to call validate_required before you get the default string "cannot be empty".

In addition, you can set the error message in detail for each field in violation:

 @permitted [:name, :phone, :url] @parameter_types %{name: :string, phone: :string, url: :string} def signup_changeset(params) do cast({%{}, @parameter_types}, params, @permitted) |> validate_required(:name, message: "Dude. You need an address.") |> validate_required(:phone, message: "You must have a name.") |> validate_required(:url, message: "We need a valid URL for your homepage.") end 
0
source

All Articles