tegmentum

tegmentum

Embeds_many ids not submitted in params

I’m having trouble updating a embeds_many field. My setup is the following:

<.simple_form
for={@form}
id="draft_order-form"
phx-target={@myself}
phx-change="validate"
phx-submit="save"
>
  <.inputs_for :let={item} field={@form[:items]}>
    <.input type="number" field={item[:quantity]} min={0} max={25}/>
  </.inputs_for>
</.simple_form>

That gives me the following markup

<div phx-feedback-for="draft_order_creation_form[items][0][quantity]" class="phx-no-feedback">
  <input type="number" name="draft_order_creation_form[items][0][quantity]" id="draft_order_creation_form_items_0_quantity" value="1" max="25" min="0">
</div>

My understanding is that i need the item.id to correctly update the changeset, but the params i receive from the “validate” event handler don’t include ids for items. I expect that this markup is making sure the ids make it to the params, but that is not the case

%{
  "_target" => ["draft_order_creation_form", "items", "0", "quantity"],
  "draft_order_creation_form" => %{
    "items" => %{"0" => %{"_persistent_id" => "0", "quantity" => "2"}},
  }
}

Any help would be much appreciated

First Post!

thiagomajesk

thiagomajesk

Have you tried including a hidden input with the item ID inside inputs_for?

Last Post!

tegmentum

tegmentum

I noticed the following hidden input is being added by inputs_for

<input type="hidden" name="draft_order_creation_form[items][0][_persistent_id]" value="0">

But this is the changeset i’m turning into a form with to_form, why wouldn’t there be an id in the params ?

the form: %Phoenix.HTML.Form{
  source: #Ecto.Changeset<
    action: :validate,
    changes: %{
      items: [
        #Ecto.Changeset<
          action: :insert,
          changes: %{
            id: "01J3TDKVKQNFGMMM44FP0N8074",
            name: "400g / Einzeldose",
            image: "a",
            quantity: 1,
            price: Decimal.new("2.99"),
            variant_id: "01J3TDKVKQNFGMMM44FP0N8074"
          },
          errors: [],
          data: #Pdex.DraftOrderCreationForm.Item<>,
          valid?: true
        >
      ],
      shipping: #Ecto.Changeset<
        action: :insert,
        changes: %{method: "dpd"},
        errors: [price: {"can't be blank", [validation: :required]}],
        data: #Pdex.DraftOrders.Shipping<>,
        valid?: false
      >,
      discount: #Ecto.Changeset<
        action: :insert,
        changes: %{unit: :money},
        errors: [value: {"can't be blank", [validation: :required]}],
        data: #Pdex.DraftOrders.Discount<>,
        valid?: false
      >
    },
    errors: [
      customer_id: {"can't be blank", [validation: :required]},
      shipping_address_id: {"can't be blank", [validation: :required]},
      billing_address_id: {"can't be blank", [validation: :required]}
    ],
    data: #Pdex.DraftOrderCreationForm<>,
    valid?: false
  >,
  impl: Phoenix.HTML.FormData.Ecto.Changeset,
  id: "draft_order_creation_form",
  name: "draft_order_creation_form",
  data: %Pdex.DraftOrderCreationForm{
    order_id: nil,
    customer_id: nil,
    shipping_address_id: nil,
    billing_address_id: nil,
    note: nil,
    market_id: "01J3TCZA758GVB40QD7RWD6DTB",
    tags: [],
    discount: nil,
    shipping: nil,
    items: []
  },
  action: nil,
  hidden: [],
  params: %{
    "customer_id" => "",
    "customer_id_text_input" => "",
    "discount" => %{
      "_persistent_id" => "0",
      "reason" => "",
      "unit" => "money",
      "value" => ""
    },
    "items" => [
      %{
        "id" => "01J3TDKVKQNFGMMM44FP0N8074",
        "image" => "a",
        "name" => "400g / Einzeldose",
        "price" => "2.99",
        "product_name" => nil,
        "quantity" => 1,
        "variant_id" => "01J3TDKVKQNFGMMM44FP0N8074"
      }
    ],
    "items_text_input" => "",
    "market_id" => "01J3TCZA758GVB40QD7RWD6DTB",
    "note" => "",
    "shipping" => %{"_persistent_id" => "0", "method" => "dpd", "price" => ""},
    "tags_empty_selection" => "",
    "tags_text_input" => ""
  },
  errors: [
    customer_id: {"can't be blank", [validation: :required]},
    shipping_address_id: {"can't be blank", [validation: :required]},
    billing_address_id: {"can't be blank", [validation: :required]}
  ],
  options: [method: "post"],
  index: nil
}

Where Next?

Popular in Discussions Top

mbenatti
Following https://github.com/tbrand/which_is_the_fastest |&gt; https://raw.githubusercontent.com/tbrand/which_is_the_fastest/master/imgs...
New
und0ck3d
Hello everyone! A few days ago I’ve created a topic here about how people were creating CMSs with Elixir and Phoenix. I’ve been studying...
New
pillaiindu
In django there is a cache framework backed by memcached. Rails also puts a lot of emphasis on caching, and even the idea of russian-doll...
New
owaisqayum
I have a sample string sentence = "Hello, world ... 123 *** ^%&amp;*())^% %%:&gt;" From this string, I want to only keep the integers, ...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
cvkmohan
The upcoming Phoenix 1.6 release looks very interesting. Became a habit to watch the commits - and - what they are bringing in. phx.gen...
New
arcanemachine
https://nitter.net/josevalim/status/1744395345872683471 https://twitter.com/josevalim/status/1744395345872683471
New

Other popular topics Top

grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54006 488
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44139 214
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New

We're in Beta

About us Mission Statement