Ecto模型更改集错误

时间:2015-06-25 15:09:41

标签: elixir phoenix-framework ecto

我有一个带字符串字段的模型,它必须至少有两个字符长:

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

问题是changeset.errors在正确填充时会返回:

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

这是一个错误,如果不是我怎么能插入这个元组?

1 个答案:

答案 0 :(得分:4)

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)).