Kernael
Kernael

Reputation: 3280

Ecto models changeset errors

I have a model with a string field, that must be at least two characters long :

def changeset(model, params \\ :empty) do
  model
  |> cast(params, @required_fields, @optional_fields)
  |> validate_length(:name, min: 2)
end

The problem is that changeset.errors, when rightfully filled, returns :

[name: {"should be at least %{count} characters", 2}]

Is it a bug, if not how can I interpolate this tuple ?

Upvotes: 3

Views: 1593

Answers (1)

José Valim
José Valim

Reputation: 51429

If you are using it with Phoenix, it will be automatically taken care for you in form_for/4 or when generating a JSON structure. Otherwise, you need to traverse it yourself and call String.replace(string, "%{count}", Integer.to_string(count)).

Upvotes: 4

Related Questions