dalhorinek
Form with nested array of fields without changeset in Phoenix 1.7
Hello.
I’m working on a system with multiple different widgets in a dashboard. I don’t have widget specific schema, I would like to keep this only using a form definition as there is no need for any validation or complex logic.
But I’m struggling with adding a dynamic rows for a nested form, I can’t get it work with to_form functionality introduced in Phoenix 1.7.
my_form = to_form(%{
"some_key" => "123",
"properties" => [
%{ "key" => "some_user_key", "value" => "some_user_value" },
%{ "key" => "another_user_key", "value" => "another_user_value" }
]
})
And I’m trying to setup the form for the nested properties
I’ve tried something like
<.inputs_for :let={f_nested} field={@my_form[:properties]} default={[]}>
<.input name="key" field={f_nested[:key] />
<.input name="value" field={f_nested[:value] />
</.inputs_for>
But this doesn’t work, it works if the properties would be a map, not an array.
I assume I need to iterate over the values, but not use how. Do I need to use inputs_for function instead of the component and iterate over the result?
What is a correct way just using the to_form achieve this? Or do I need to use ecto schemas to achieve this?
Thank you in advance.
Most Liked
japplegame
It’s a little late, but I ran into a similar problem and found a solution.
It expects two-element tuples in the list. The first element is used only for sorting, and the second is the data for the nested form:
my_form = to_form(%{
"some_key" => "123",
"properties" => [
{1, %{ "key" => "some_user_key", "value" => "some_user_value" }},
{2, %{ "key" => "another_user_key", "value" => "another_user_value" }}
]
})
Or you can use a map instead of a list (default attribute of inputs_for still must be a list)
my_form = to_form(%{
"some_key" => "123",
"properties" => %{
1 => %{ "key" => "some_user_key", "value" => "some_user_value" },
2 => %{ "key" => "another_user_key", "value" => "another_user_value" }
}
})
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








