Ecto - validate presence of associated model

人走茶凉 提交于 2019-12-03 10:45:40
José Valim

There isn't anything yet. But you can run these validations yourself in your changeset function:

def changeset(model, params) do
  model
  |> cast(...)
  |> validate_bar_association()
end

def validate_bar_association(changeset) do
  bar = changeset.model.bar
  cond do
    bar == nil ->
      add_error changeset, :bar, "No bar"
    length(bar) < 5 ->
      changeset
    true ->
      add_error changeset, :bar, "waaaay too many"
  end
end

We do want to make nested associations better but there are other items higher up on our priority list. :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!