我创建了一个如下所示的模式:
schema "countries" do field :code, :string field :en, :string field :de, :string field :it, :string field :fr, :string timestamps end现在我想创建一个接受地图列表的变更集功能。 我该如何定义它? 这是对的(我会说这是错的):
def changeset_all(model,[])do
那么如何验证传递的参数是否包含地图?
第二个问题是,我怎样才能遍历地图列表,在获得所有传递值的变更后? 我会这样做:
def changeset_all(model, [params]) do for param <- [params] do model |> cast(param, [:code, :en, :de, :it, :fr]) |> validate_required([:code, :en]) |> validate_length(:code, max: 3) |> unique_constraint(:code) end end我希望我的问题很清楚。
I created a schema that looks as follow:
schema "countries" do field :code, :string field :en, :string field :de, :string field :it, :string field :fr, :string timestamps endNow a I want to create changeset function that would accept a list of map. How can I define it? Is this right(I would say it is wrong):
def changeset_all(model, []) do
Then how can I validate if the passed parameter contains maps or not?
The second question is, how can I loop through the list of maps, that after I would get a changeset of all pass values? I would do like this:
def changeset_all(model, [params]) do for param <- [params] do model |> cast(param, [:code, :en, :de, :it, :fr]) |> validate_required([:code, :en]) |> validate_length(:code, max: 3) |> unique_constraint(:code) end endI hope my questions are clear.
最满意答案
def changeset_all(model, [params]) do只接受1个元素的列表。 要接受任何列表,您可以:
def changeset_all(model, params) when is_list(params) do要创建变更集列表,只需for param <- params 。 最终代码应如下所示:
def changeset_all(model, params) when is_list(params) do for param <- params do model |> cast(param, [:code, :en, :de, :it, :fr]) |> validate_required([:code, :en]) |> validate_length(:code, max: 3) |> unique_constraint(:code) end end那么如何验证传递的参数是否包含地图?
你真的不需要这样做,就像传递一个无效的参数一样, cast会处理抛出错误。 如果你想这样做,你可以遍历列表并使用if来检查它是否是一张地图:
for param <- params if !is_map(param) do raise "#{param} is not a map" end end for param <- params do # changeset stuff end我建议单独创建一个changeset/2函数,然后在changeset_all/2使用Enum.map调用它:
def changeset(model, params) do model |> cast(params, [:code, :en, :de, :it, :fr]) |> validate_required([:code, :en]) |> validate_length(:code, max: 3) |> unique_constraint(:code) end def changeset_all(model, list) do list |> Enum.map(fn params -> changeset(model, params) end) enddef changeset_all(model, [params]) do will only accept a list of 1 element. To accept any lists, you can do:
def changeset_all(model, params) when is_list(params) doTo create a list of changesets, just do for param <- params. The final code should look like:
def changeset_all(model, params) when is_list(params) do for param <- params do model |> cast(param, [:code, :en, :de, :it, :fr]) |> validate_required([:code, :en]) |> validate_length(:code, max: 3) |> unique_constraint(:code) end endThen how can I validate if the passed parameter contains maps or not?
You don't really need to do this as if you pass an invalid parameter, cast will handle throwing an error. If you want to do this, you can loop through the list and use if to check if it's a map:
for param <- params if !is_map(param) do raise "#{param} is not a map" end end for param <- params do # changeset stuff endI would recommend creating a changeset/2 function separately and then calling it using Enum.map in changeset_all/2:
def changeset(model, params) do model |> cast(params, [:code, :en, :de, :it, :fr]) |> validate_required([:code, :en]) |> validate_length(:code, max: 3) |> unique_constraint(:code) end def changeset_all(model, list) do list |> Enum.map(fn params -> changeset(model, params) end) end更多推荐
发布评论