使用embeds_many时删除空嵌套表单

时间:2017-10-24 12:23:31

标签: forms elixir phoenix-framework ecto

我有一个嵌套表单,其中只需要1/5提供的选项。但是,如果用户提交的表单中只填写了一个嵌套表单,则Ecto会将变更集返回为无效。

我在我的控制器中添加了一个scrub_params插件,但看起来这并没有删除空的嵌套表单。

plug(:scrub_params, "inventories" when action in [:create])

凤凰城有没有办法删除空表格?

Conn Params params [“inventoryories”]:

%{"payment_invoice_type" => "mail",
  "delivery_methods" =>
  %{"0" => %{"cost" => "10.00", "description" => "Del Option 1"},
    "1" => %{"cost" => "20.00", "description" => "Del Option 2"},
    "2" => %{"cost" => "30.00", "description" => "Del Option 3"},
    "3" => %{"cost" => nil, "description" => nil}, # should be discarded
    "4" => %{"cost" => nil, "description" => nil}}, # should be discarded
  "payment_method" => "cash",
}

变更集结果:

#Ecto.Changeset<action: nil,
 changes: %{payment_invoice_type: "mail,
   delivery_methods: [#Ecto.Changeset<action: :insert,
     changes: %{cost: #Decimal<10.00>, description: "Del Option 1"}, errors: [],
     data: #Book.Store.ShippingMethod<>, valid?: true>,
    #Ecto.Changeset<action: :insert,
     changes: %{cost: #Decimal<20.00>, description: "Del Option 2"}, errors: [],
     data: #Book.Store.ShippingMethod<>, valid?: true>,
    #Ecto.Changeset<action: :insert,
     changes: %{cost: #Decimal<30.00>, description: "Del Option 3"}, errors: [],
     data: #Book.Store.ShippingMethod<>, valid?: true>,
    #Ecto.Changeset<action: :insert, changes: %{},
     errors: [description: {"can't be blank", [validation: :required]},
      cost: {"can't be blank", [validation: :required]}],
     data: #Book.Store.ShippingMethod<>, valid?: false>,
    #Ecto.Changeset<action: :insert, changes: %{},
     errors: [description: {"can't be blank", [validation: :required]},
      cost: {"can't be blank", [validation: :required]}],
     data: #Book.Store.ShippingMethod<>, valid?: false>],
   payment_method: "cash"},
 errors: [],
 data: #Book.Store.Inventory<>, valid?: false>

父架构:

架构“库存”做     field:payment_invoice_type,:string     field:payment_method,:string     embeds_many:shipping_options,Book.Store.ShippingOptions     时间戳()   端

父变更集:

def changeset(%Inventory{} = inventory, attrs) do
  inventory
  |> cast(attrs, [:payment_invoice_type, :payment_method])
  |> validate_required([:payment_invoice_type, :payment_method])
  |> cast_embed(:shipping_methods, required: true)
end

嵌入式架构:

  @primary_key false
  embedded_schema do
    field :description, :string
    field :cost, :decimal
  end

  def changeset(%DeliveryOption{} = delivery_option, attrs) do
    delivery_option
    |> cast(attrs, [:description, :cost])
    |> validate_required([:description, :cost])
    |> validate_length(:description, min: 5, max: 75)
  end

1 个答案:

答案 0 :(得分:0)

我认为没有办法在Ecto中自动删除这些条目。您可以像这样轻松地手动删除条目:

inventories = Map.update!(inventories, "delivery_methods", fn map ->
  :maps.filter(fn _key, value -> !match?(%{"cost" => nil, "description" => nil}, value), map)
end)

这将删除delivery_methodscost都设置为description的所有nil值。

或者,以下内容将删除所有值均设置为nil的条目:

inventories = Map.update!(inventories, "delivery_methods", fn map ->
  map |> Map.values |> Enum.all?(&is_nil/1)
end)