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